How to Make Redirect with Htaccess in Webserver

There were many ways to redirect to the web, you can use functions in HTML or PHP, you can also use .htaccess. Yes, we can make redirects using the .htaccess file, of course, if we use the Apache web server.


Why Need a Redirect?

There are many reasons why a redirect is needed by a web manager. Among other things, due to changes in the URL structure or permalink, there is a change in the domain so that a dynamic redirect is needed so that visitors do not "get lost", and many other reasons. To create or manage this redirect, you can edit the .htaccess file in your public_html directory.

Redirect Old Domain to New Domain

Next is the rule to switch the old domain to a new domain, but the URL structure does not change.
RewriteCond %{HTTP_HOST} ^olddomain.com [NC,OR] RewriteCond %{HTTP_HOST} ^www.olddomain.com [NC] RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R=301,NC]
The code above will transfer old-domain.com to new-domain.com, as well as when we access old-domain.com/article to new-domain.com/article.

Redirect WWW to non-WWW

Almost the same as the rule above.
RewriteEngine on RewriteCond %{HTTP_HOST} ^domain.com [NC] RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301,NC]
Also applies to the opposite, just change it.

Redirect Certain URLs in the Same Domain

For example, we want to redirect domain.com/old-permalink to domain.com/new-permalink, then the rule.
Redirect 301 /old-permalink/new-permalink

Redirect Certain URLs in Different Domains

For example, we want to redirect old-domain.com/old-permalink to new-domain.com/new-permalink, then the rule
Redirect 301 /old-permalink newdomain.com/new-permalink
Newer Oldest

Related Posts

Post a Comment

Subscribe Our Newsletter