Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite: top-level redirects add extra slash after domain name

         

cameronM

7:26 pm on Nov 7, 2004 (gmt 0)

10+ Year Member



I am trying to allow redirection from URLs like this:

www.foo.com/about

to go to www.foo.com/xyz/about.php

However, I am getting www.foo.com//xyz/about.php which works but is annoying.

A problem is that all of the mod_rewrite docs assume that you are in a subdirectory (e.g. /xyz) and not in the top-level directory (e.g. /). If I don't add a RewriteBase directive, Apache returns the full physical path which, of course, fails.

It appears that the domain name + the trailing slash are held by the server and then recombined with the RewriteBase parameter + the rewritten URL. Since I can't specify a null RewriteBase parameter, I get an extra slash.

My .htaccess looks like this:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^about$ xyz/about.php [R,L]

Ideas?

Thanks,

Cameron

P.S. Elsewhere, someone suggested using the search term of /about ... which doesn't work since the initial slash doesn't exist by the time the regex is applied.

jdMorgan

8:28 pm on Nov 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Cameron,

Welcome to WebmasterWorld!

This code has a problem, and it's not clear if you want to do an external redirect or an internal rewrite.

For an external redirect, which returns a response telling the browser to ask for the resource at a new URL, use:


Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^about$ [b]http://www.example.com/[/b]xyz/about.php [R[b]=301[/b],L]

For an internal rewrite, which simply "maps" the internal path to the requested URL, use:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^about$ /xyz/about.php [[b]L[/b]]

Basically, if the [R] flag is used, you must supply a canonical URL (beginning with http://<domain>). If you want an internal rewrite, then you should supply only a local URL-path and should not use [R]. See the mod_rewrite documentation for details.

If changing the code does not work, you may have a server configuration problem -- possibly related to virtual host naming syntax and/or to the UseCanonicalName setting. If you do not have access to httpd.conf, ask your host to set UseCanonicalName off to see if that fixes the problem.

Jim

cameronM

8:38 pm on Nov 7, 2004 (gmt 0)

10+ Year Member



Jim,

Thanks for the welcome!

And thanks so much for the information. You were right on the money. My brain had processed the server vs. client redirect capability but hadn't put that together with the need to mod the rewritten URL depending upon which style I was using.

Removing the [R] flag results in the desired behavior.

Thanks again!

Cameron