Redirect using .htaccess 301

One of the better and easier ways to redirect is using .htaccess 301 redirect. What is “.htaccess”?

“.htaccess is a configuration file for use on web servers running the Apache Web Server software. When a .htaccess file is placed in a directory which is in turn ‘loaded via the Apache Web Server’, then the .htaccess file is detected and executed by the Apache Web Server software. These .htaccess files can be used to alter the configuration of the Apache Web Server software to enable/disable additional functionality and features that the Apache Web Server software has to offer. These facilities include basic redirect functionality, for instance if a 404 file not found error occurs, or for more advanced functions such as content password protection or image hot link prevention.” htaccess-guide.com

Always be sure to upload .htaccess files in ascii mode.

To redirect a single page:

Redirect 301 /oldpage.html http://www.yourdomain.com/newpage.html

To redirect an entire site:

Redirect 301 / http://www.yourdomain.com/

To redirect /index.php to yourdomain.com/

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 redirect /index.html to yourdomain.com/

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

To redirect non-www to www — It’s a good idea not to confuse search engines. More information on “canonical url errors“.

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

To redirect www to non www — the opposite of the above.

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L,R=301]
Tweet This Post

SocialTwist Tell-a-Friend

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading ... Loading ...

blog comments powered by Disqus

Free Email Subscription

Poll

21 Free Random Links

Have a new site and want to show off and get linked? Send us an email and we'll link it here for free.