How To Use PHP htaccess for 301 Redirects and Domain Name Forwarding

Safe and Effective Web Page Management

The .htaccess file instructs the web server how to handle certain requests from a web browser or search engine bot. .htaccess can handle multiple requests, including blocking IP addresses and search engine bots altogether. The 301 redirect command is used when the established location of a web page URL is changed and the preservation of search engine indexing and rank is desired. It returns the server response “moved permanently” and is a much safer solution than a “meta refresh.”

For domain name forwarding, most website owners have more than one version of a domain name pointing to a single site. An example is a numerical version of a domain name like Section1Wrestling that points to the alpha version of SectionOneWrestling. Some domain registrars have domain name forwarding services, but be aware that some do not provide a search engine friendly redirection. It’s much safer to do it yourself through your .htaccess file and eliminate any potential problems.

Step 1

To create your .htaccess file, open up a text editor like Notepad in Windows. You can either save the file as .htaccess now or name and save it after you enter your data into the file. To save the file, click file, save as, then enter .htaccess into the “save as” description box. In the "Save as type" pulldown box, select "All Files" and click the "Save" radio button. There are no other file extensions associated with the file.

Step 2

To add the entry into your .htacess file for a 301 redirect “moved permanently:”

redirect 301 /articles/MyFirstArticle.html http://www.MyMainDomainName.com/articles/MyNewArticleLocation.html

 

Step 3

To add the code into your .htaccess file for domain name forwarding, enter the following:

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{HTTP_HOST} ^www.MySecondDomainName.com [NC]

RewriteRule ^(.*)$ http://www.MyMainDomainName.com/$1 [L,R=301]

Step 4

If you haven’t already done so, you should also forward your non-WWW domain name version to the full WWW URL. This will prevent the search engines from indexing two versions of your site and hitting the website with duplicate content penalties.

To add the code into your .htaccess file to forward your non-WWW URL to your full WWW URL enter:

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{HTTP_HOST} ^MyMainDomainName.com [NC]

RewriteRule ^(.*)$ http://www.MyMainDomainName.com/$1 [L,R=301]

Step 5

Upload the .htaccess file to the server into the root directory of the website. The root directory is the main folder where your Index.html file is located. You’re almost finished, but there’s one more thing to do. And that’s test the functionality of your .htaccess file by typing in the old URLs and web pages to make sure they display the correct pages.


In the event that you already have an .htaccess file on your site, simply pull down a copy and edit it with any changes you need to make, then upload the edited file back up to the server and test the functionality.

 

Share this article!

Follow us!

Find more helpful articles: