Forum Moderators: phranque

Message Too Old, No Replies

Subdomains

         

branmh

10:11 pm on May 2, 2009 (gmt 0)

10+ Year Member




What I would like to do is to have all my subdomains point to my main www directory, but I need to be able to use the .htaccess rewrites for the subdomains. Is this even possible to do and how would I do it?

branmh

11:34 pm on May 2, 2009 (gmt 0)

10+ Year Member



I starting to get a point here:

RewriteBase /
RewriteCond %{HTTP_HOST} dev.mydomain.com$
RewriteCond %{REQUEST_URI} !dev/
RewriteRule ^(.*)$ dev/$1
RewriteRule /^dev/test.php$ /pathtomymainscriptsdirectory/test.php?%{query_STRING} [NC,L,S=1]

but on my rewriterule /dev/test.php it want's to send me to /dev/pathtomymainscriptsdirectory/test.php instead of /pathtomymainscriptsdirectory/test.php.

How to get rid of the /dev/ in my rewriterule?

g1smd

12:44 am on May 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The RewriteConds will only apply to the single RewriteRule that directly follows. The first rule presumably needs [L] adding to it.

The second rule should never run, because the first one matches (.*), 'all'.

Note that /^ is not a valid syntax.

branmh

2:33 am on May 3, 2009 (gmt 0)

10+ Year Member



Can you explain in more detail?

Also, if I already rewrite already for test.php somewhere else but I also have a rewrite for /dev/test.php, how would I get the /dev/test.php to load under dev.mysite.com?

branmh

2:40 am on May 3, 2009 (gmt 0)

10+ Year Member



I got it working to a point....

RewriteBase /
RewriteCond %{HTTP_HOST} dev.mysite.com$
RewriteCond %{REQUEST_URI} !dev/
RewriteRule ^$ /pathtomyscripts/devindex.php?&%{query_STRING} [NC,L,S=1]
RewriteRule ^index.php$ /pathtomyscripts/devindex.php?&%{query_STRING} [NC,L,S=1]

if you goto dev.mysite.com and www.mysite.com it loads the right script, but now my main www.mysite.com/index.php is redirecting to the devindex.php instead of index.php,

RewriteRule ^$ /pathtomyscripts/index.php?&%{query_STRING} [NC,L,S=1]
RewriteRule ^index.php$ /pathtomyscripts/index.php?&%{query_STRING} [NC,L,S=1]

What is the fix for this?

g1smd

5:52 pm on May 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What do you mean by "redirecting". All of the above code is for "rewrites", not for "redirects".

branmh

2:46 am on May 4, 2009 (gmt 0)

10+ Year Member



Its not loading the right index.php, example when dev.mysite.com/index.php loads up devindex.php but when I goto www.mysite.com/index.php it loads up the devindex.php instead of the index.php file.

Does this make since now?

jdMorgan

2:08 pm on May 4, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You were warned above that RewriteConds only apply to *one* RewriteRule.

Therefore, you must add RewriteConds to your second rule if you do not wish that rule to apply to all hostnames.


RewriteCond %{HTTP_HOST} dev\.mysite\.com
RewriteCond %{REQUEST_URI} !/dev/
RewriteRule ^$ /pathtomyscripts/devindex.php [L]
#
[i]RewriteCond %{HTTP_HOST} dev\.mysite\.com
RewriteCond %{REQUEST_URI} !^/pathtomyscripts[/i]
RewriteRule ^index\.php$ /pathtomyscripts/devindex.php [NC,L]

A better solution, if and only if "pathtomyscripts" is identical to "/dev/", might be to combine both rules into one:

RewriteCond %{HTTP_HOST} dev\.mysite\.com
RewriteCond %{REQUEST_URI} !^/pathtomyscripts
RewriteRule ^(index\.php)?$ /pathtomyscripts/devindex.php [NC,L]

The same applies to your third and fourth rule, which seem to be identical, except that rule #3 looks for "^$" and rule #4 looks for "^index\.php$". Use the power of regular expression patterns to combine two these rules into one:

RewriteCond %{REQUEST_URI} !^/pathtomyscripts
RewriteRule ^(index\.php)?$ /pathtomyscripts/index.php [NC,L]

Be careful, here. I am only guessing that your first and second rule are meant to be the same except that rule #1 rewrite "^$" and rule #2 rewrites "^index\.php$". If this is not true, then you cannot combine rules #1 an #2.

Also, note the addition of the RewriteCond to prevent recursive rewriting.

However, if this is the case, then you will end up with only two rules:


# If development hostname requested, rewrite requests for "/" or "index.php" to /pathtomyscripts/devindex.php
RewriteCond %{HTTP_HOST} dev\.mysite\.com
RewriteCond %{REQUEST_URI} !^/pathtomyscripts
RewriteRule ^(index\.php)?$ /pathtomyscripts/devindex.php [NC,L]
#
# If any other hostname is requested, rewrite requests for "/" or "index.php" to /pathtomyscripts/index.php
RewriteCond %{REQUEST_URI} !^/pathtomyscripts
RewriteRule ^(index\.php)?$ /pathtomyscripts/index.php [NC,L]

Note that I also cleaned up several other mistakes and inefficiencies, notably the unnecessary [S=1] flags and inclusion of "?&%{QUERY_STRING}. The [S=n] flag is meaningless when used with [L], and query strings pass through mod_rewrite unchanged if no query string is attached to the RewriteRule substitution path. If you wish to append new query data to the existing query data instead of replacing it, you can use the [QSA] flag. But that does not seem to be the case here, so you likely don't need [QSA] either.

Final note: I have added comments to these two rules. If the comments are correct, then the code is likely correct. The usefulness of comments is two-fold: First, comments help you organize your thoughts, and secondly, they remind you of the purpose of the rules when you look at them again in three years.

Jim