Forum Moderators: phranque

Message Too Old, No Replies

Subdomain rewrite rules

Tried searching this site, but none works

         

craig1972

7:18 am on Apr 7, 2004 (gmt 0)

10+ Year Member



I have tried searching this site for subdomain htaccess rewrite rules, and there are several solutions. None works. Some just give me a "DNS error, site not found" while others give me a 500 Error.

I want to forward any URLs in this pattern:


[<xyz>.MYDOMAIN.COM...]

Should forward to


[MYDOMAIN.COM...]

I need to replace the "xyz" with whatever word I wish, with only one exception - the word "secure" -- because I already use [secure.MYDOMAIN.COM...] for something else. Other than this word, users can type anything and they should be forwarded to "status.php" in my root folder.

How to specify this in my .htaccess file?

Thanks for any advice/ideas/pointers!

jdMorgan

3:01 pm on Apr 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



craig1972,

What you are looking for is called a "back-reference" and is documented in the mod_rewrite [httpd.apache.org] RewriteCond and RewriteRule directives. Test the requested HTTP_HOST ("domain name") with RewriteCond, and then back-reference that by using the back-reference variable %1 in your RewriteRule.

Other than that small change, the code will be identical to the many code samples posted here for the purpose of redirecting www domains to non-www domains, or vice-versa.

Jim

craig1972

3:28 pm on Apr 7, 2004 (gmt 0)

10+ Year Member



thanks jdmorgan.

here is one simple example i am using now, even this is not working:


RewriteEngine on
RewriteCond %{HTTP_HOST} photo\.MYDOMAIN\.COM [NC]
RewriteCond %{REQUEST_URI}!^/photo
RewriteRule (.*) /photo/$1 [L]

the error i get: it just tells me domain not found. as if it is looking for "photo.MYDOMAIN.COM" which of course is not in the DNS server.

any pointers would be great! thanks!

jdMorgan

5:25 pm on Apr 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In order for this to work, you'll have to add the subdomain to your DNS "zone" file. Otherwise, the requests for the subdomain won't ever get to your server.

The new entry in your zone file will look something like this:

xyz.yourdomain.com. IN A 192.168.0.1

This "points" the xyz subdomain to the IP address of your server.

Once, that's done, then you can pass the requested subdomain to your script with something like this:


RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.+)\.mydomain\.com [NC]
RewriteCond %{HTTP_HOST) !^www\.
RewriteRule .* /status.php?%1 [L]

Your status.php code will then need to extract the requested "page" from the REQUEST_FILENAME server variable.

The second RewriteCond is used so that requests for your "www" subdomain are not passed to the script -- In most cases, you'll want to treat www.yourdomain.com and yourdomain.com as the same thing. This may not be what you want, but I'm showing it as an example.

Jim

craig1972

10:58 am on Apr 8, 2004 (gmt 0)

10+ Year Member



this is brilliant jim, thanks a bunch! i am sure this will help someone in the future too.

best regards