Forum Moderators: phranque

Message Too Old, No Replies

Re-writing sub domains as variables in script

so foo.bar.com becomes bar.com/script.php/?foo=foo

         

Sevans777

5:28 pm on Apr 5, 2005 (gmt 0)

10+ Year Member



I have a mod rewrite problem
I have a domain - Domain.com. with wildcard subdomains set.
I have a script - script.php that accepts two variables foo and bar.
foo is always a subdomain bar is always passed as bar.html

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.

sitz

7:19 pm on Apr 5, 2005 (gmt 0)

10+ Year Member



The second example fails because the requested URL doesn't have '.html' in it and the third because the third RewriteCond fails to match the host header.

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)?$

...if you KNOW that you're not going to have any '.' characters in the hostname. The RewriteRule will match on either ^/$ or ^/$SOMETHING.html; if an HTML file is requested, it will be stored in $2. In these cases, some of your query strings may be blank; for instance, you may wind up with a rewritten path which looks like:

/path/to/script.php?s=&q=foo

This shouldn't cause problems, but you should be aware of the fact.

jdMorgan

7:39 pm on Apr 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Steve,

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]

You had an undefined case, which is what to do with requests for www.domain.com, where "www" is technically a subdomain of domain.com. I assume that you wish to treat this as if "www" were not present, as this is the usual case.

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

Sevans777

10:36 pm on Apr 5, 2005 (gmt 0)

10+ Year Member



Thanks to you both for the help.

Jim,
Your soloution worked without any modification - more than that I actually understood the code.
Jim - your Fantastic.

Steve.