mod_rewrite Help

Associate
Joined
13 Nov 2003
Posts
1,567
Location
Manchester
Hi All

I am working a site and need something fairly complicated from mod_rewrite

The core PHP files will be powering 8 different versions of the site. The content will change depending on the version of the site.

The URLs will be in the format www.siteurl.com/sitename

Where sitename will change for each version.

Basically what I need to do is to make all of the pages work through that URL.

For example - this is the code to get the front page displaying

Code:
RewriteEngine on
RewriteRule ^sitetwo/$ /index.php?site_id=2

This is easy enough but I dont want to have to create a rule for each one.

So I need some code that is going to redirect the whole lot - whilst retaining the query strings that are used. Thats where I get stuck.

Can anyone suggest anything that might help?

Thanks
Aaron
 
Unless you can use the site name itself in the query string rather than just an ID then there's no way you can do it via mod_rewrite. After all, how would it know which ID to use without you telling it?
 
Hey

Thanks for the reply

I could use the sitename and then just use some code to get the ID from the site name

I don't mind replicating the rewrite rules for each site - just don't want to have to write a rule for every file within that site - if that makes sense

Cheers
Aaron
 
Ah, you want to automate it per file, rather than per site. I get you now.

Is the URI following the site name unmodified? E.g. example.com/sitename/index.php?foo=bar

If so, then this should do the trick (untested):

Code:
RewriteEngine On
RewriteRule ^sitetwo/(.+?)\?(.*) $1?site_id=2&$2
RewriteRule ^sitethree/(.+?)\?(.*) $1?site_id=3&$2

... etc.

Or, if you want something that'll work for all sites (though requiring code to look up the site from its name):

Code:
RewriteEngine On
RewriteRule ^(.+?)/(.+?)\?(.*) $1?site_name=$2&$3

Again, untested, so may need fixing :p
 
Last edited:
Yeah my bad, try these instead:

Code:
RewriteEngine On
RewriteRule ^sitetwo/(.*) $1?site_id=2&%{QUERY_STRING} [L]
RewriteRule ^sitethree/(.*) $1?site_id=3&%{QUERY_STRING} [L]

Or:

Code:
RewriteEngine On
RewriteRule ^(.+?)/(.*) $2?site_name=$1&%{QUERY_STRING}

Tested these and they should work :)

Edit: wait, no they don't
 
Last edited:
Back
Top Bottom