Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite adding two slashes at end of URL

         

regulatethis

1:33 am on Apr 24, 2010 (gmt 0)

10+ Year Member



I'm trying to redirect example.com to www.example.com using a basic rewrite rule. My problem is when I type example.com, I get redirected to http://www.example.com//. I don't know why it adds two slashes at the end, it's boggling my mind. There are other rewrite rules in play which may or may not be affecting this. Here's my vhost config:


<VirtualHost *:80>
ServerAdmin webmaster@example.com
ServerName www.example.com
ServerAlias example.com
DocumentRoot /sites/www.example.com/site

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule (.*)$ http://www.example.com/$1 [R=301]
</IfModule>

ErrorLog /sites/www.example.com/logs/error.log
CustomLog /sites/www.example.com/logs/access.log combined

<Directory /sites/www.example.com/site/>
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>

AllowOverride none
Order allow,deny
allow from all
</Directory>

<Directory /sites/www.example.com/site/app/>
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>

AllowOverride none
Order allow,deny
allow from all
</Directory>

<Directory /sites/www.example.com/site/app/webroot/>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>

AllowOverride none
Order allow,deny
allow from all
</Directory>
</VirtualHost>


I'm about to jump out the window out of frustration here, can someone please help me?

g1smd

7:23 am on Apr 24, 2010 (gmt 0)

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



RewriteRule
can see the leading slash of the part part of the URL request when used in the
httpd.conf
file.

So you have
[b]/[/b]something
in
$1
and you're redirecting to
www.example.com[b]/[/b]$1
hence two slashes.

regulatethis

7:43 am on Apr 24, 2010 (gmt 0)

10+ Year Member



So how do I correct that?

I type http://example.com with no trailing slash and I get http://www.example.com//

regulatethis

7:49 am on Apr 24, 2010 (gmt 0)

10+ Year Member



Hmm, I just changed it to:

RewriteRule (.*)$ http://www.example.com$1 [R=301]


... and it seems to work... but that looks strange. Is that the way I have to do it?

regulatethis

8:01 am on Apr 24, 2010 (gmt 0)

10+ Year Member



Looks like this works:

RewriteRule ^/(.*)$ http://www.example.com/$1 [R=301]


and seems a little cleaner. Thanks for the tip!

g1smd

8:25 am on Apr 24, 2010 (gmt 0)

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




One note. Always add the [L] flag to every RewriteRule directive unless you are absolutely sure that it should be omitted.