Redirecting With .htaccess

Need to get your visitors from one spot to the next? Have one of yur pages changed locations? .htaccess redirection may be what you are looking for. There are many types of redirection in .htaccess, so I’ll make sure to explain each of them, and their uses!

301 Redirection

A 301 redirect is for when you permanently move a page’s location. A 301 redirect not only redirects the visitor to the new page, but also informs search engines like Google that the page has moved. You will not be punished for using this, and Google will index the new page to show to your visitors.

301 Redirect


Redirect 301 /old_page.html http://www.domain.com/new_page.html

Note: the /old_page.html does not have to exist. Make sure to replace domain.com with your real domain name, and “http” with “https” if your site uses SSL.

Note: An .htaccess file located in a sub-directory overrides any duplicate rules from previous .htaccess files. For example, if you have a .htaccess file located in the root defining a 404 and 403 error page, and another .htaccess located in the “test” folder defining only a 404 error page, any files and folders in the “test” folder will use the 404 page defined in the "test" .htaccess file, and the 403 page defined in the root .htaccess file.

Advertisement

TinkerAd

302 Redirection

A 302 redirect is for when you temporarily move a page’s location. A 302 redirect not only redirects the visitor to the new page, but also informs search engines like Google that the page has only moved for a short period of time, and will come back to it's original location soon.

302 Redirect


Redirect 302 /old_page.html http://www.domain.com/new_page.html

Note: the /old_page.html does not have to exist. Make sure to replace domain.com with your real domain name, and “http” with “https” if your site uses SSL.

Note: An .htaccess file located in a sub-directory overrides any duplicate rules from previous .htaccess files. For example, if you have a .htaccess file located in the root defining a 404 and 403 error page, and another .htaccess located in the “test” folder defining only a 404 error page, any files and folders in the “test” folder will use the 404 page defined in the "test" .htaccess file, and the 403 page defined in the root .htaccess file.

Error Pages

Since it is technically a type of redirection, I will include it here. Error pages are shown to visitors when an error like “Not Found” (404) or “Forbidden” (403) is encountered on your website. To learn more, check out the article on error pages.


Redirection from one domain to another while keeping the file path

Changed domain names? Some of your visitors may have your old pages bookmarked, and you don’t want to just redirect them to your new homepage! .htaccess makes it simple to redirect visitors from one domain to another while keeping the rest of the URL path intact. For example, if I went to olddomain.com/contect.html, I would be redirected to newdomain.com/contact.html.

Some Content


RedirectMatch 301 ^/(.*)$ http://domain.com/$1

Remember to replace domain.com with your real domain name, and “http” with “https” if your site uses SSL.

Note: The page on the old domain does not have to exist, so you don’t have to keep your files updated in two locations! If the file does not exist on the new domain, users will get your 404 Not Found page on your new domain.

Note: An .htaccess file located in a sub-directory overrides any duplicate rules from previous .htaccess files. For example, if you have a .htaccess file located in the root defining a 404 and 403 error page, and another .htaccess located in the “test” folder defining only a 404 error page, any files and folders in the “test” folder will use the 404 page defined in the "test" .htaccess file, and the 403 page defined in the root .htaccess file.

Advertisement

TinkerAd

Conclusion

That's it! Super simple, but also super useful to know. It will definitely save you time, and it’s even SEO friendly (Always a plus)! See you next time.

Advertisement

Article by Tinkerman

Related Articles