Htaccess 301 Redirect Apache Server

While there are many ways in implementing 301 redirect code in your .htaccess page, here I am putting the most commonly used way for different purposes.

If you found same address of your website with www and with out www that are indexed in google, then your site will get penalized very soon. You need to move your website to either www version or non www version. It is very Simple.

If you want to move WWW to Non-WWW then use the below code. (you do not have to replace anything, just use as it is)

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

If you want to move from Non-WWW to WWW then use the below code
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]

If you wanna remove the trailing ‘/’ from the url:

RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]

If you wanna move a single web page to a new page:

Redirect 301 /oldpage.html


If you wanna move the entire website to a new location:

Redirect 301 /


Another Example to move www to “non-www”

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^
RewriteRule (.*)$1 [R=301,L]


Another example to move “non-www” to www:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^ [NC]
RewriteRule ^(.*)$$1 [L,R=301]


One more Important example to move “” to “”

Options +FollowSymLinks
RewriteEngine on
# index.php to /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /.*index.php HTTP/
RewriteRule ^(.*)index.php$ /$1 [R=301,L]


To move index.html to index.php

RewriteEngine on
RewriteRule index.html index.php [NC,R]

Leave a Reply

Your email address will not be published.

You May Also Like