Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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]

I have two questions:

1) There's a double slash issue:

  • When I go to http://www.example.com it correctly rewrites the URL to http://www.example.com/
  • When I go to http://www.example.com/somepage, it correctly rewrites the URL to http://www.example.com/somepage
  • If I go to http://example.com, it rewrites the URL to http://www.example.com// (double final slash)
  • If I go to http://example.com/somepage, it correctly rewrites it to http://www.example.com/somepage

Any tips to fix it?

2) Is my configuration good for SEO?

like image 914
Mark Avatar asked Nov 24 '10 19:11

Mark


People also ask

What does double slash mean in pseudocode?

Particularly as a double slash in written work usually means "new line here".

Why is there a double slash in path?

Two backslashes are used as a prefix to a server name (hostname). For example, \\a5\c\expenses is the path to the EXPENSES folder on the C: drive on server A5.

What do double forward slashes mean?

Lines beginning with // are comments and are ignored during action execution.

What is mod_rewrite?

mod_rewrite is an Apache module that allows for server-side manipulation of requested URLs. mod_rewrite is an Apache module that allows for server-side manipulation of requested URLs. Incoming URLs are checked against a series of rules. The rules contain a regular expression to detect a particular pattern.


2 Answers

Fixed with:

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

because $1 by default contains the index path /

like image 89
Mark Avatar answered Sep 17 '22 01:09

Mark


RewriteRule ^\/?(.*)$ http://www.mydomain.com/$1 [R=301,L]
like image 40
Rahly Avatar answered Sep 17 '22 01:09

Rahly