Forum Moderators: phranque

Message Too Old, No Replies

subdirectory mod_rewrite problem

         

carrot63

5:36 am on Sep 14, 2005 (gmt 0)

10+ Year Member



Hi all,

I've just moved a site to a different host, and I'm having a maddening problem getting a rewrite to work - not helped by the fact I seem to have forgotten some of the basics.

In the .htaccess in the root directory I have the following to rewrite reuests for domain.co.uk to www.domain.co.uk:

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain\.co\.uk [NC]
RewriteRule ^(.*)$ [domain.co.uk...] [R=permanent,L]

This works fine, including for all pages in subdirectories except for one, which contains a rewrite to direct netscape browsers to another page. So, in /directory/ I have:

rewriteOptions inherit
RewriteBase /
RewriteCond %{HTTP_USER_AGENT} Mozilla.* [NC]
RewriteCond %{HTTP_USER_AGENT} .*netscape.* [NC]
RewriteRule page.html /directory/page_ns.html [R,L]

The problem is that a request for domain.co.uk/directory/page.html comes out as www.domain.co.uk/page_ns.html generating a 404 error

It seems it is partially rewriting the request, but losing the directory somewhere in the process. I'm having trouble seeing why the backreference isnt containing the whole path.

I'm fairly sure this worked OK on the old host, but its possible I may not have tested this URL for the domain.co.uk to www.domain.co.uk rewrite.

I'm confident I am being a little thick and missing the obvious - any help appreciated.

wizzud

12:27 pm on Sep 14, 2005 (gmt 0)

10+ Year Member



What happens if, in the sub-directory, you change the rewrite base to '/directory/' and remove '/directory/' from the rewrite rule?

jdMorgan

1:01 pm on Sep 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you do not specify a canonical name for a redirect, then the server may get confused (for lack of a better term). The correct syntax for a redirect would be:

RewriteRule ^page\.html$ http://www.domain.co.uk/directory/page_ns.html [R=301,L]

However, I question whether you need or want to use an external redirect here, since it involves forcing the client browser to do two requests for each resource -- Seems a bit unfair to Netscape users...

You could simply do an internal rewrite to provide alternate content at the same URL for NS users:


RewriteRule ^page\.html$ /directory/page_ns.html [L]

Jim