How to set up a 301 redirect using htaccess

How to set up a 301 redirect using htaccess

20.Jul.2021

How to set up a 301 redirect using htaccess

How to set up a 301 redirect using htaccess you ask? Better yet, why would one do such a thing?

Both are questions I was asking myself late last week. A friend had mentioned that there were SEO implications to having both urls.by and www.urls.by floating on the net.

See, Google et al treat urls.by and www.urls.by as separate sites. Technically, www is a subdomain, similar to this.that.com, where that.com is the domain, and this is the subdomain.

In other words, www.urls.by and urls.by might be stealing page rank and search juice from each other. This will not do!

This is where the 301 redirect comes in. A redirect simply tells any browser that’s looking for urls.by to automatically go to www.urls.by. Setting up is pretty simple, if you’re comfortable editing files on a server.

Assuming you know your way around an ftp program (I use Transmit, for the mac), point your self at your hosting account. Look in the root or public_html folders (setup may be different, according to your host) and try to find a file called .htaccess Note the . before the name. This means that it’s a hidden file. You may have to look for “Show Hidden Files” in your ftp program. Try looking in Preferences or under the View menu.

Not all blog programs require .htaccess, and not all hosting plans allow it’s use. So you may be out of luck using this technique. Not to fear though! Urls.by lists 7 different ways to create a 301 redirect.

Assuming you’ve found the .htaccess file, make a copy of it somewhere for backup. Next, open the file in a text editor. The following is from https://urls.by/blog/301-redirect-using-htaccess (reposted here with permission).

Redirect to www (htaccess redirect)

Create a .htaccess file with the below code, it will ensure that all requests coming in to urls.by will get redirected to www.urls.by

The .htaccess file needs to be placed in the root directory of your old website (i.e the same directory where your index file is placed)

Options +FollowSymlinks

RewriteEngine on

rewritecond %{http_host} ^urls.by [nc]

rewriterule ^(.*)$ http://www.urls.by/$1 [r=301,nc]

Please REPLACE  urls.by and www.urls.by with your actual domain name.

Note* This .htaccess method of redirection works ONLY on Linux servers having the Apache Mod-Rewrite moduled enabled.

You can stick this at the end of your existing .htaccess file. Now replace the version on the server with your modified file (you did make a backup of the original file, right?). Test it out.

You can use a 301 redirect for a variety of things, including moving domains (just point your old URL to the new site), directing search engines when you move a file, and more. Creating a 301 redirect is a gentle way to enter the dark world of .htaccess. With a little knowledge, and a good command of copy and paste, you can customize your .htaccess file to do some very useful things. For a great list of stupid htaccess tricks, check out Perishable Press.