Forum Moderators: phranque
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!
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
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!
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]
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