views:

270

answers:

2

How do you set a 301 redirect in .htaccess to add the forward slash to your document root if someone links to you without it?

According to the research I have done most search engines consider the following URL's as two different URL's.

mydomain.com (no forward slash)

mydomain.com/ (forward slash)

I've tried this (plus many others):

RewriteRule ^$ http://www.mydomain.com/ [R=301,L]

That throws it into a loop loading the page over and over.

A: 

I think you drew incorrect conclusions out of your research. For HTTP, the Root-URL without forward slash is specified to be equal to the forward slash:

Note that the absolute path cannot be empty; if none is present in the original URI, it MUST be given as "/" (the server root). [RFC 2616 Section 5.1.2]

Hence, if the original URL is only a domain name that does not end with a forward slash (i.e. the absolute path would be empty), that URL will be extended by a forward slash. You don't have to do anything.

As for your problem: Due to the subtleies of mod_rewrite, the first slash is ommited, so your RewriteRule captured the root URL and sent the requester in a redirection-loop.

nd
A: 

You can try something like that

ensure you are matching a domain without a / and if not do the redirection.

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