Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite - sudomain and .html file

mod rewrite subdomain redirection

         

bobosse

2:43 am on Sep 24, 2004 (gmt 0)

10+ Year Member



Hi everyone,

I have been trying to figure out this one all day long and hope to get some input from the forum tonight.

Here is what I try to do: I want to write:

[1-2.mysite.com...]

which should run:

file.php?var1=1&var2=2&var3=3
That's what I have but it does nto work:

RewriteEngine On
RewriteRule ^http://(.*)-(.*)\.mysite/(.*).html$ /file.php?var1=$1&var2=$2&var3=$3

Any idea how to make this work out?
Thanks. JM

jdMorgan

3:05 am on Sep 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



JM,

Welcome to WebmasterWorld!

The problem is that RewriteRule does not "see" domain names, it only sees the local URL-path. So, you have to handle the domain name separately using RewriteCond, and access the back-references using %1 - %9 instead of $1 - $9.


RewriteCond %{HTTP_HOST} !^www\.mysite\.com
RewriteCond %{HTTP_HOST} ^(www\.)?([^-]+)-([^.]+)\.mysite\.com
RewriteRule ^([^.]+)\.html$ /file.php?var1=%1&var2=%2&var3=$1

Note that the domain name patterns must not be end-anchored, and that the order of the RewriteConds is important; back-references can only be made to the last-matched RewriteCond.

I assumed that you do not wish to rewrite requests where the subdomain is equal to "www" only. If this is not true, delete the first RewriteCond. The second RewriteCond will accept subdomains in the form
abc-defg.mysite.com
-or-
www.abc-defg.mysite.com
(In other words, the www is optional. The www not passed to the script if it is followed by a subdomain in wx-yz form. Only the wx and yz will be passed to the script, even if preceded by www.)

I also assumed that this code is intended for use in .htaccess. If you want to use it in httpd.conf, add a leading slash to the RewriteRule pattern.

The references cited in our forum charter may be useful if the above code is not clear.

Jim