How To Redirect 301 URL by .htaccess

You can redirect a URL permanently (301 redirects) using .htaccess the file. To create a 301 redirect, you need to add a redirect rule in the .htaccess file.

Here is an example of how to redirect old-url to new-url using .htaccess:

Redirect 301 /old-url /new-url

In the above code, old-url is the URL you want to redirect, and new-url is the URL where you want to redirect. The 301 indicates that the redirection is permanent, which will update search engines and browsers with the new URL.

You can add multiple redirect rules in .htaccess file for multiple URLs that you want to redirect.

Additionally, you can also use regular expressions in .htaccess file to redirect URLs based on patterns. This is useful if you want to redirect multiple URLs that match a particular pattern.

Here is an example of how to redirect URLs based on a pattern:

RewriteEngine On
RewriteRule ^old-url(.*)$ /new-url$1 [R=301,L]

And Final Code is given below.

RewriteEngine On

RewriteCond %{HTTPS} off 
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1%{REQUEST_URI} [R=301,QSA,NC,L]

Redirect 301 /old-url /new-url

In the above code, ^old-url(.*)$ is the pattern for the URLs you want to redirect, and /new-url$1 is the URL where you want to redirect? $1 is a back-reference to the matched pattern, which allows you to preserve any additional URL path information that follows old-url.

Make sure to back up your .htaccess file before making any changes and test the redirect thoroughly to ensure it works as expected.

One response to “How To Redirect 301 URL by .htaccess”

Leave a Reply

Your email address will not be published. Required fields are marked *