Htaccess 301 redirect

Hi,

I want to setup 301 redirect on following urls to one url given below:

http://www.example.com
http://www.example.com/index.htm
http://example.com
http://example.com/index.htm

To

http://example.com

Is this possible using htaccess?

thanks

The old location of the file has to be the absolute path from the root of your server. The new location should use http. So for example, if you want to move a file called http://www.example.com from the root of your site to a subdirectory called products you would use :

[B]The exact URL entered above[/B]: This will redirect the search spiders and users to the exact URL you entered in the “Redirect To:” example. For instance, if you somebody went to http://www.example.com/index.htm, they would go to http://example.com.

[B]A directory below this one[/B]: Freaky Friday images aside, this redirects a parent directory to a child directory. For instance, if someone typed in http://www.example.com/index.htm to access the site and you wanted the request to be redirected to a child directory named “example.com”, then they would be sent to http://example.com.

OR

The old location of the file has to be the absolute path from the root of your server. The new location should use http. So for example, if you want to move a file called productreview.html from the root of your site to a subdirectory called example you would use :

Redirect 301 /http://www.example.com/index.htm http://example.com

Hi CW!

Welcome to SitePoint!

Well, that was a good though long-winded explanation then you missed the correct format of the Redirect statement:

Redirect {optional code} {internal absolute directory/file} {internal or external absolute redirection}

The point is that the Redirect statement you used should NOT have the protocol along with the internal absolute directory/file.

Redirect 301 [COLOR="#FF0000"]/http://www.example.com[/COLOR]/index.htm http://example.com

Diz,

Why would you ever want to redirect http://example.com to http://example.com? That’s an infinite loop!

Specificity:

Redefining your stated objective:

  1. Remove www. from domain
  2. Remove index.htm from the URI (IMHO, a bad thing to do especially as your host may have Apache configured to display the DirectoryIndex).

The short answer is “yes.”

Try:

RewriteEngine on

# Remove www
RewriteCond %{HTTP_HOST} ^www\\. [NC]
RewriteRule .? http://example.com%{REQUEST_URI} [R=301,L]

# Remove index.htm from DocumentRoot
RewriteRule ^index\\.htm$ http://example.com [R=301,L]

Of course, all this is explained in the mod_rewrite tutorial linked as an Article in my signature.

Regards,

DK