Forum Moderators: phranque

Message Too Old, No Replies

RewriteRule help

[L] not working as expected

         

boxfan

12:14 am on Feb 21, 2006 (gmt 0)

10+ Year Member



Hello,

I have the following in my htaccess file

RewriteEngine On
RewriteRule ^application\.html$ http://www.domain2.com/app_instructions/index.html [L]
RewriteRule ^registerproducts\.html$ http://www.domain2.com/register_product/index.html [L]
redirectMatch 301 ^(.*)$ http://www.domain2.com
redirectMatch permanent ^(.*)$ http://www.domain2.com

What I'm trying to do is redirect all requests for any URL on domain1 to domain2 except for a few select pages I want to go to selected pages on domain2.

The problem is if a person clicks on application.html or registerproducts.html they are taken to domain2.com instead of http://www.domain2.com/app_instructions/index.html or http://www.domain2.com/register_product/index.html. I thought by placing the [L] after the first two entries that Apache would stop looking for further entries in the htaccess file.

Any help would be appreciated.

John

[edited by: jdMorgan at 12:35 am (utc) on Feb. 21, 2006]
[edit reason] De-linked [/edit]

jdMorgan

12:28 am on Feb 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



John,

The main problem is that you're trying to mix directives from two modules; mod-alias and mod_rewrite. This is OK in theory, but if the URLs handled by these two modules' directives are not mutually-exclusive, then there's a problem: You cannot control the order that the modules process their directives it.

To be clear, each Apache module parses your code in turn, and executes the directives that it understands. So while Apache will obey the order that you specify for directives handled within a specific module, the server config determines whether all mod_alias directives are executed followed by all mod_rewrite directives, or vice-versa.

Also, the [L] flag, while stopping mod_rewrite processing for the current pass through your code, has no effect whasoever on mod_alias.

Jim

boxfan

1:29 am on Feb 21, 2006 (gmt 0)

10+ Year Member



Ok, I see now. So can I just replace the redirectmatch's with

RewriteRule ^(.*)$ [domain2.com...] [R=permanent,L]

and get the same results except the first two rewrites will work properly?

John

jdMorgan

1:52 am on Feb 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



One more question: Are these domains hosted on separate (possibly virtual) servers, or are both hosted on this one server?

Jim

boxfan

3:57 am on Feb 21, 2006 (gmt 0)

10+ Year Member



Same server, different accounts. One physical server but each on its own virtual server.

John

jdMorgan

4:13 am on Feb 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, that simplifies things, since you won't need to test {HTTP_HOST} to find out if you're *already on* domain2. You have to do that if both hostnames resolve to the same directory on the same virtual host; Otherwise, you get an 'infinite' redirection loop.

The code then would look like this:

 
RewriteEngine on
RewriteRule ^application\.html$ http://www.domain2.com/app_instructions/index.html [R=301,L]
RewriteRule ^registerproducts\.html$ http://www.domain2.com/register_product/index.html [R=301,L]
RewriteRule (.*) http://www.domain2.com/$1 [R=301,L]

where $1 in the third rule's URL substitution back-references the stored value of the text matching the parenthesized regular expression in the pattern. So, it copies the URL-path into the new URL.

If you've got a DirectoryIndex directive in domain2/app_instructions and domain2/register_product, then by all means, get rid of the "index.html" in the substitution URLs, and just redirect to <directory_name>/. This keeps the URL shorter, and avoids calling the same file by two names -- /index.html and "/". For reasons why one name is better than two, search for "duplicate content penalty."

Jim

boxfan

4:57 am on Feb 21, 2006 (gmt 0)

10+ Year Member



Thank you very MUCH!

John