Redirect URL using htaccess

Last Updated: June 27, 2023By

June 27, 2023

In order to redirect a URL to another URL using .htaccess, you can use the Redirect or RedirectMatch directive provided by Apache’s mod_alias, or the RewriteRule directive provided by mod_rewrite. Here are examples of both.

  1. Using Redirect:
Redirect 301 /oldpath http://www.yoursite.com/newpath

This will redirect http://www.yoursite.com/oldpath to http://www.yoursite.com/newpath with a 301 (Moved Permanently) HTTP status code.

  1. Using RewriteRule:
RewriteEngine On
RewriteRule ^oldpath$ http://www.yoursite.com/newpath [R=301,L]

This will also redirect http://www.yoursite.com/oldpath to http://www.yoursite.com/newpath with a 301 HTTP status code. Note that the RewriteEngine On line is necessary to enable the rewrite engine if it’s not already enabled.

Remember to replace “http://www.yoursite.com”, “oldpath”, and “newpath” with your actual domain and path names. Also, .htaccess file must be placed in the root directory of your website (or in the directory you want to have the redirect rule applied to).

Please keep in mind that the use of .htaccess for these kinds of operations, while convenient, can degrade performance, especially on high-traffic sites. It’s better to put these configurations directly into server configuration files if possible.

editor's pick

news via inbox

Nulla turp dis cursus. Integer liberos  euismod pretium faucibua