Forum Moderators: phranque
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?
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?
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]
RewriteCond %{HTTP_HOST} dev\.mysite\.com
RewriteCond %{REQUEST_URI} !^/pathtomyscripts
RewriteRule ^(index\.php)?$ /pathtomyscripts/devindex.php [NC,L]
RewriteCond %{REQUEST_URI} !^/pathtomyscripts
RewriteRule ^(index\.php)?$ /pathtomyscripts/index.php [NC,L]
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]
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