Forum Moderators: phranque
I need to rewrite
1,foo.domain.com/bar.html as domain.com/script.php?foo=foo&bar=bar
2,foo.domain.com as domain.com/script.php?foo=foo
3,domain.com/bar.html as domain.com/script.php?bar=bar
I spent all of yesterday playing with this and have come up with
RewriteEngine On
RewriteCond %{REQUEST_URI}!^/index\.php
RewriteCond %{HTTP_HOST}!^www\.
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com
RewriteRule (.*)(.*).html script.php?s=%1&q=$1
That works with scenario 1
But not 2 and 3
I have to admit that its completely bolted together from other webmasterworld examples,
So any further help would really be appreciated.
Steve.
Off the top of my head (I don't have access to a system I can test this on right this second), you could do something like this:
RewriteEngine on
RewriteCond %{REQUEST_URI}!^/index\.php$
RewriteCond %{HTTP_HOST}!^www\.
RewriteCond %{HTTP_HOST} ^(.+\.)?domain\.com(:80)?$ [NC]
RewriteRule ^/((.+).html)?$ /path/to/script.php?s=%1&q=$2 [L]
This performs your earlier checks to ensure that the request uri isn't for /index.php and that the Host: header doesn't start with 'www.'; note the use of the 'NC' flag, which will make this Condition case-insensitive (host headers can come in mixed case). The RewriteCond will grab leading hostname information and store it in %1 if it's there; otherwise, it will be blank. I tend to be leary of regexes like '(.+)', though, so you may want ot substitute something like
^([^.]+\.)?domain.com(:80)?$
/path/to/script.php?s=&q=foo
This shouldn't cause problems, but you should be aware of the fact.
Welcome to WebmasterWorld!
You have four defined cases:
1) No subdomain or www subdomain with no resource name (filename).
2) No subdomain or www subdomain with resource name.
3) Non-www subdomain with no resource name.
4) Non-www subdomain with resource name.
RewriteEngine On
#
# Case 1: No subdomain, no resource name
# If domain is "domain.com" or "www.domain.com"
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com
# Rewrite blank request to script with no parameters
RewriteRule ^$ /script.php? [L]
#
#Case 2: No subdomain with resource name
# Prevent rewrite loop if we've already rewritten to script.php
RewriteCond %{REQUEST_URI} !^/script\.php$
# If domain is "domain.com" or "www.domain.com"
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com
# Rewrite to script with q parameter set to requested resource
RewriteRule ^([^.]+)\.html$ /script.php?q=$1 [L]
#
# Case 3: Subdomain with no resource name
# If subdomain is not (only) "www"
# Create a back-reference to required subdomain
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.domain\.com
# Rewrite blank request to script with s parameter set to subdomain.
RewriteRule ^$ /script.php?s=%2 [L]
#
# Case 4: Subdomain with resource name
# Prevent a rewrite loop if we've already rewritten to script.php
RewriteCond %{REQUEST_URI} !^/script\.php$
# If subdomain is not (only) "www"
# Create a back-reference to required subdomain
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.domain\.com
# Rewrite to script with s parameter set to subdomain and q parameter set to requested resource.
RewriteRule ^([^.]+)\.html$ /script.php?s=%2&q=$1 [L]
A related undefined case is what to do if you receive a request for "www.subdomain.domain.com". Again, I assumed you'd want to treat this the same as a request for "subdomain.domain.com". You could also allow for "subdomain.www.domain.com" in a similar way.
The remaining case which is not defined is what should happen to non-html resource requests such as images. The code above does not rewrite these requests in any way, which I presume is what you want.
The main trick here is to get organized by identifying all the cases. Then you can go after each one.
It's possible to write this code more elegantly, but it makes the code much less clear.
Several useful documents are cited in our forum charter [webmasterworld.com].
Jim