Mark
Mark

Reputation: 70010

mod_rewrite and double slash issue

I applied the following mod_rewrite rule in Apache2 to redirect from non-www to www:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

However, there's a double slash issue:

Is my configuration good for SEO?

Upvotes: 22

Views: 31640

Answers (5)

Alberto Martinez
Alberto Martinez

Reputation: 2670

That is because the root path is /, and you are appending whatever you get in RewriteRule (the first case works fine because it doesn't match the condition so no rewrite is performed).

You can try something like this:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
# for the home page
RewriteRule ^/$ http://www.example.com/ [R=301,L]
# for the rest of pages
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

Upvotes: 1

user502515
user502515

Reputation: 4454

Actually, you will always have double slashes due to

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

combined with the fact that REQUEST_URI (that you are matching on) normally contains a starting slash. What you can try is RewriteRule ^(.*)$ http://example.com$1, and then send a broken HTTP request GET foo HTTP/1.0 and see if Apache deals with it properly.

Upvotes: 7

Rahly
Rahly

Reputation: 1511

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

Upvotes: 22

Mark
Mark

Reputation: 70010

Fixed with:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com$1 [R=301,L]

because $1 by default contains the index path /

Upvotes: 41

Dennis Winter
Dennis Winter

Reputation: 2037

Putting a slash into your pattern should resolve this issue:

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

Upvotes: 3

Related Questions