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.

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.

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.

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.

Advertisement

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.

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.

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 Author

Related Articles

Deny Access to Hidden Files with .htaccess

Is your website ready to go live? It’s always a good idea to block people from accessing files they shouldn't, like your .htaccess file for example! This code will block access to any files beginning ...

How To Set Error Pages On Your Website Using .htaccess

Setting your error pages is important, so if someone tries to access a page that does not exist, or a page they are forbidden to see, they will get a friendly error message, plus some helpful links fr...

Finding, Creating, and Editing .htaccess files

.htaccess is amazing! If you haven’t read the <a href="/webhosting/htaccess/what-is-htaccess">introduction to .htaccess</a>, check that out first (Unless you already know the amazing things .htaccess c...

Advertisement

All code and content © 2024. Contact Us to learn more.