Forum Moderators: phranque

Message Too Old, No Replies

rewrite domain names to folders

Apache RewriteRule RewriteCond folders

         

JayWBeck

10:53 pm on Aug 25, 2012 (gmt 0)

10+ Year Member



Both my domain names use the same IP address.
I want:
www.example1.com --> www.example3.com/folder1
www.example2.com --> www.example3.com/folder2

I have written:

--------------
RewriteEngine on#
#
# Handle www.example1.com
#
RewriteCond %{HTTP_HOST} ^(www.)?example1\.com$ [nc]
RewriteRule (.*) http://example3.com/$1 [R=301]
RewriteRule ^$ folder1 [L]
#
#
# Handle www.example2.com
#
RewriteCond %{HTTP_HOST} ^(www.)?example2\.com$ [nc]
RewriteRule (.*) http://example3.com/$1 [R=301]
RewriteRule ^$ folder2 [L]
-----------------

when I type into a browser: www.example1.com
I get www.example3.com/folder1
great!

when I type into a browser: www.example2.com
I still get: www.example3.com/folder1 and not www.example3.com/folder2

Do you see an error?

lucy24

10:38 am on Aug 26, 2012 (gmt 0)

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



Sure do. You've got:

Rule with Condition

Rule without Condition

Rule with Condition

Rule without Condition


By the time you reach the fourth Rule, the second Rule has already put you in /folder1. This in turn means that your request no longer fits the ^$ pattern, so Rule 4 doesn't apply.

And that's why you will always hear people telling you to leave a blank line after every RewriteRule. Among other things, it helps you remember that Conditions apply only to the immediately following rule-- whether it executes or not.

You also have Redirects without accompanying [L]. Frankly these make me nervous. In rare cases it is necessary. But here a better solution is to change the structure of the rules.

I suppose it's no use asking why this isn't being handled by DNS so you don't have to go through all this jiggery-pokery? Rules that apply to only one domain are happier if they are in their own htaccess or <Directory> section.