mod_rewrite rules for live site

mod_rewrite rules for live site

am 27.10.2007 19:11:44 von dennis.sprengers

* I'm trying to work out some rewrite rules that should do the
following:

case 1: a request to mysite.com:
rewrite http://www.mysite.com/files/sheets.ppt
to http://www.mysite.com/sites/default/files/sheets.ppt

case 1 - n: a request to othersite.com:
rewrite http://www.othersite.com/files/sheets.ppt
to http://www.othersite.com/sites/othersite/files/sheets.ppt

So, if a request comes in to mysite.com, /files/-urls need to be
rewritten do /sites/default/files/sheets/ppt, since mysite is the
default website on the server. Surfing to a file on any other website
that is hosted on the server, needs to be rewritten to /sites/
/files/sheets.ppt

Sheets.ppt is of course just an example of a file file.

* I first tested my CMS (for which I need these rules) on my own
little server @home, and found these rules to be working OK after
spending hours on forums on apache and mod_rewrite:

# http://localhost/mysite/files/* > /mysite/sites/default/files/*
RewriteCond %{REQUEST_URI}-default ^/mysite/.+-(default)$ [NC,OR]
# http://loclahost//files/* > //sites/
/files/*
RewriteCond %{REQUEST_URI}-default ^(.+)(default)$ [NC,OR]

But now I have no clue on where to begin anymore, since it has been a
long time I researched mod_rewrite. Could somebody help me out? Any
help is appreciated :-)

Re: mod_rewrite rules for live site

am 27.10.2007 21:13:19 von HansH

schreef in bericht
news:1193505104.620426.104470@o80g2000hse.googlegroups.com.. .
>* I'm trying to work out some rewrite rules that should do the
> following:
>
> case 1: a request to mysite.com:
> rewrite http://www.mysite.com/files/sheets.ppt
> to http://www.mysite.com/sites/default/files/sheets.ppt
>
> case 1 - n: a request to othersite.com:
> rewrite http://www.othersite.com/files/sheets.ppt
> to http://www.othersite.com/sites/othersite/files/sheets.ppt
>
> So, if a request comes in to mysite.com, /files/-urls need to be
> rewritten do /sites/default/files/sheets/ppt, since mysite is the
> default website on the server. Surfing to a file on any other website
> that is hosted on the server, needs to be rewritten to /sites/
> /files/sheets.ppt
>
Try these untested lines:
RewriteEngine ON
RewriteMap lc int:tolower
# IN GENERAL
# take second level of domainname ...
# - any or none prefix accepted
# - do not distinct tlds -.com, .org, .eu-
# (still need appropriate serveraliasses)
RewriteCond %{HTTP_HOST} \.([^\.]+)\.[^\.]+$
# ... and insert it in lowercase into the URI
RewriteRule ^(/files/.*) /sites/${lc:%{%1}}$1

# EXCEPTION
# change a specified domain to default
RewriteRule ^/sites/mysite/(files/.*) /sites/default/$1

You may need to add some flags to either or both rules, like [PT,QSA]

HansH

Re: mod_rewrite rules for live site

am 27.10.2007 21:32:39 von shimmyshack

On Oct 27, 6:11 pm, dennis.spreng...@gmail.com wrote:
> * I'm trying to work out some rewrite rules that should do the
> following:
>
> case 1: a request to mysite.com:
> rewritehttp://www.mysite.com/files/sheets.ppt
> tohttp://www.mysite.com/sites/default/files/sheets.ppt
>
> case 1 - n: a request to othersite.com:
> rewritehttp://www.othersite.com/files/sheets.ppt
> tohttp://www.othersite.com/sites/othersite/files/sheets.ppt
>
> So, if a request comes in to mysite.com, /files/-urls need to be
> rewritten do /sites/default/files/sheets/ppt, since mysite is the
> default website on the server. Surfing to a file on any other website
> that is hosted on the server, needs to be rewritten to /sites/
> /files/sheets.ppt
>
> Sheets.ppt is of course just an example of a file file.
>
> * I first tested my CMS (for which I need these rules) on my own
> little server @home, and found these rules to be working OK after
> spending hours on forums on apache and mod_rewrite:
>
> #http://localhost/mysite/files/*> /mysite/sites/default/files/*
> RewriteCond %{REQUEST_URI}-default ^/mysite/.+-(default)$ [NC,OR]
> #http://loclahost//files/* > //sites/
> /files/*
> RewriteCond %{REQUEST_URI}-default ^(.+)(default)$ [NC,OR]
>
> But now I have no clue on where to begin anymore, since it has been a
> long time I researched mod_rewrite. Could somebody help me out? Any
> help is appreciated :-)

i saw your post on the comp.lang.php and i was confused about what
you are trying to do (read: are you sure you want to do this!?)
are you sure you want to rewrite requests on some host for

files/some_file.ext

to

sites/some_host/files/some_file.ext

it suggests that you have a funny document root structure.


for instance why on earth would you want to do this?
http://www.mysite.com/files/sheets.ppt
->
http://www.mysite.com/sites/default/files/sheets.ppt
(where default is actually the site www.mysite.com)

sites/default should be private and not shown in the url.

what would happen if someone went to
http://www.mysite.com/sites/othersite/files/sheets.ppt

I hope that encourages you to think of a more standard solution.

Are you intending to have a central "file repository" for all sites,
if not why not just have a directory for each site inside the document
root for each site?

Re: mod_rewrite rules for live site

am 27.10.2007 22:44:08 von dennis.sprengers

On Oct 27, 9:32 pm, shimmyshack wrote:

> Are you intending to have a central "file repository" for all sites,
> if not why not just have a directory for each site inside the document
> root for each site?

I want to run multiple websites from one codebase. Mysite.com is the
default website and code repository. All other websites will be
symlinked to this codebase. Rougly, the dirtree @ mysite.com looks
like this:

modules
sites
default
images
files
other_site
images
files
system
includes

Let's say I'm an editor of other_site.com. For me, it's not
interesting to know my website runs on the codebase of another
website. I expect my files to be accessible through other_site.com/
files, so that if I'm typing some text, I can link to < a href="files/
file.ppt">, not to < a href="/sites/other_site/files/file.ppt">. So
these are all files that are accessible anyway through the web. I
don't think there's a security risk here.

That aside, I value your views on this. What would be a more common
way, then?

Re: mod_rewrite rules for live site

am 28.10.2007 01:12:57 von HansH

schreef in bericht
news:1193517848.450237.222520@z9g2000hsf.googlegroups.com...
> I want to run multiple websites from one codebase. Mysite.com is the
> default website and code repository. All other websites will be
> symlinked to this codebase. Rougly, the dirtree @ mysite.com looks
> like this:
>
> modules
> sites
> default
> images
> files
> other_site
> images
> files
> system
> includes
>
> Let's say I'm an editor of other_site.com. For me, it's not
> interesting to know my website runs on the codebase of another
> website. I expect my files to be accessible through other_site.com/
> files, so that if I'm typing some text, I can link to < a href="files/
> file.ppt">, not to < a href="/sites/other_site/files/file.ppt">. So
> these are all files that are accessible anyway through the web. I
> don't think there's a security risk here.
There is hardly a risk of unintentional publishing unless you use 'real'
redirects.

To keep the browser ignorant, I've interpreted your initial question
How to rewrite http://www.othersite.com/files/sheets.ppt
into http://www.othersite.com/sites/othersite/files/sheets.ppt
as
How to remap http://www.othersite.com/files/sheets.ppt
to /sites/othersite/files/sheets.ppt
and omitted the [R]flag.


If the default is there to always fill in the gaps of (any) other_site,
consider
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteRule ^/sites/[^/]+/(files/.*) /sites/default/$1
to rewrite the mapping to the default for missing files -but not folders-.

If the rewrites are setup in .htaccess, change any
RewriteRule ^/sites...
into
RewriteRule ^sites

Be aware publishing identical content on multiple sites might get you banned
by GOOGLE.

> That aside, I value your views on this. What would be a more common
> way, then?
Your structure is pritty close to the more common set up
default
images
files
modules
system
includes
other_site
images
files
where default and other_site are the document root of _individual_ vhosts.
It looks like you are delibaretly (re)virtualizing a single (virtual) host
into multiple sites.


HansH

Re: mod_rewrite rules for live site

am 28.10.2007 13:37:26 von shimmyshack

On Oct 27, 11:12 pm, "HansH" wrote:
> schreef in berichtnews:1193517848.450237.222520@z9g2000hsf.googlegroups .com...
>
> > I want to run multiple websites from one codebase. Mysite.com is the
> > default website and code repository. All other websites will be
> > symlinked to this codebase. Rougly, the dirtree @ mysite.com looks
> > like this:
>
> > modules
> > sites
> > default
> > images
> > files
> > other_site
> > images
> > files
> > system
> > includes
>
> > Let's say I'm an editor of other_site.com. For me, it's not
> > interesting to know my website runs on the codebase of another
> > website. I expect my files to be accessible through other_site.com/
> > files, so that if I'm typing some text, I can link to < a href="files/
> > file.ppt">, not to < a href="/sites/other_site/files/file.ppt">. So
> > these are all files that are accessible anyway through the web. I
> > don't think there's a security risk here.
>
> There is hardly a risk of unintentional publishing unless you use 'real'
> redirects.
>
> To keep the browser ignorant, I've interpreted your initial question
> How to rewritehttp://www.othersite.com/files/sheets.ppt
> intohttp://www.othersite.com/sites/othersite/files/sheets.pp t
> as
> How to remaphttp://www.othersite.com/files/sheets.ppt
> to /sites/othersite/files/sheets.ppt
> and omitted the [R]flag.
>
> If the default is there to always fill in the gaps of (any) other_site,
> consider
> RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
> RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
> RewriteRule ^/sites/[^/]+/(files/.*) /sites/default/$1
> to rewrite the mapping to the default for missing files -but not folders-.
>
> If the rewrites are setup in .htaccess, change any
> RewriteRule ^/sites...
> into
> RewriteRule ^sites
>
> Be aware publishing identical content on multiple sites might get you banned
> by GOOGLE.
>
> > That aside, I value your views on this. What would be a more common
> > way, then?
>
> Your structure is pritty close to the more common set up
> default
> images
> files
> modules
> system
> includes
> other_site
> images
> files
> where default and other_site are the document root of _individual_ vhosts.
> It looks like you are delibaretly (re)virtualizing a single (virtual) host
> into multiple sites.
>
> HansH

My view was just that you wouldn't want to actually redirect users to
a URL that revelas the underlying file structure in the way you were
trying to do, but HansH has as usual come up with an elegant way to
get what you are trying to do, done without revealing this info to
browsers anyway.

Your sites are just server aliases in that there is a single codebase,
and therefore document root, you could use (and probably do ) within
your vhost section

Server mysite.com
ServerAlias othersite.com
ServerAlias yetanothersite.org

this means that when it comes to keeping the files in separate folders
you could have a handler for serving the files (which means that the
files do not need to be in a public location on the webserver) the
handler.php script knows which host its on ($_SERVER['HTTP_HOST']) and
can request the files from the location based on that.
/private/files/host/file.ppt
this has the benefit of being a secure way to allow different users to
access different files, and various other felxible things because you
are now using code. The link to files would always be something like
this
http://www.othersite.com/sites/files/sheets.ppt
where no directory files would exist in that location (it is in /
private/files)
and there would be a single rewrite to point anything that started
files
ReWriteRule %{REQUEST_URI} ^files/(.*) /handler.php?filename=$1 [L]
you then contain the logic in the php script, which you can change
quickly
and can add complexity to when required. (I for instance use the
handler script to mirror my content away from the server if busy / to
switch to content distribution networks if one particular file gets a
lot of hits, as well as to provide per user security, bandwidth
shaping and concurrent download prevention)

anyway thats another way, but HansH is more exiting i think!

Re: mod_rewrite rules for live site

am 28.10.2007 14:55:07 von shimmyshack

http://www.othersite.com/sites/files/sheets.ppt

sorry i meant

http://www.othersite.com/files/sheets.ppt

or
http://www.mysite.com/files/sheets.ppt

as of course the actual location is found by the handler.php script
which knows $_SERVER['HTTP_HOST']

sorry for the typo

Re: mod_rewrite rules for live site

am 28.10.2007 15:33:27 von HansH

"shimmyshack" schreef in bericht
news:1193575046.953037.299230@o3g2000hsb.googlegroups.com...
> Your sites are just server aliases in that there is a single codebase,
> and therefore document root, you could use (and probably do ) within
> your vhost section
>
> Server mysite.com
> ServerAlias othersite.com
> ServerAlias yetanothersite.org
>
> this means that when it comes to keeping the files in separate folders
> you could have a handler for serving the files (which means that the
> files do not need to be in a public location on the webserver) ....
That's disliked by most if not all hosters: one will be ristrained at
best one level above the one document root.

> ... the handler.php script knows which host its on
> ($_SERVER['HTTP_HOST']) and can request the files from the location
> based on that.
:: snipped ::
> and there would be a single rewrite to point anything that started
> files
> ReWriteRule %{REQUEST_URI} ^files/(.*) /handler.php?filename=$1 [L]
> you then contain the logic in the php script, which you can change
> quickly and can add complexity to when required.
On static data I'ld rather not do myself what apache2 can do for me:
- set content-type, content-length and last-modified headers ao
- deflate on the fly
- and last but not least the performance advantage of sendfile.

> (I for instance use the handler script to mirror my content away from
> the server if busy / to switch to content distribution networks if
> one particular file gets a lot of hits, as well as to provide per user
> security, bandwidth shaping and concurrent download prevention)
Per user acces is the ONE reason to go through all hassle I dislike ...
.... That's why a script modifiing a rewritemap upon visitors logon|off
has been doing such for me - but that won't do in .htaccess anyway-

In total you're playing quite another ballgame, seems you're way beyond
virtual hosting -or use it beyond limits too ;-) -

HansH