How to create a 301 redirect?

As described in URL Canonicalization article we published in March, having a unique URL for each webpage is important in improving your "Pagerank". Canonicalization is accomplished by redirecting non-standard web pages to a preferred ("standard") webpage. There are several ways to redirect a webpage, but 301 (HTTP/1.1 Status Code, "Moved Permanently") redirect is the search engine-friendly method that passes the PageRank and search engine ranking status from the old to a new page. Here is an example of how 301 redirects can be implemented in a LAMP (Linux/Apache/Mysql/PHP) environment.

PHP Redirect

<?php
Header( "HTTP/1.1 301 Moved Permanently" ); 
Header( "Location: http://www.new-url.com" ); 
?>

Apache .htaccess: non-www to www subdomain redirect

1. Make sure your httpd.conf file has the "Directory" directive with the "AllowOverride All" setting.

<Directory /path/to/your/domain/document/root>
    AllowOverride All
</directory>

2. Enable Mod-Rewirte Module

Your Apache configuration should have the mod-rewrite module enabled, which is the default. You can verify this by perusing httpd.conf file for the following line:

LoadModule rewrite_module modules/mod_rewrite.so

3. Create a .htaccess file in the document root directory of your website with the following content.

RewriteEngine On
RewriteCond %{http_host} ^webtrafficexchange.com [nc]
RewriteRule ^(.*)$ http://www.webtrafficexchange.com/$1 [r=301,nc]

Just replace the "webtrafficexchange.com" with the domain name you're trying to redirect.

Conclusion

There are a few ways you can create 301 redirects, and the specifics of creating a 301 redirect can vary based on the web server you're using. You may use our online tool to generate 301 redirects. Additionally, be cautious when editing server configuration files, as incorrect configurations can impact your website's functionality.

Share this post

Comments (0)

    No comment

Leave a comment

All comments are moderated. Spammy and bot submitted comments are deleted. Please submit the comments that are helpful to others, and we'll approve your comments. A comment that includes outbound link will only be approved if the content is relevant to the topic, and has some value to our readers.


Login To Post Comment