0
votes

I have a Domain which I want to redirect to New Domain using 301 redirection on htaccess; I did that successfully but now the problem is, I want to redirect pages / folders of old domain to various locations of new domain. E.g.

www.olddomain.com redirect to www.newdomain.com (works) www.olddomain.com/about should redirect to www.newdomain.com/who-we-are (doesn't work)

I tried redirect 301 oldurl newurl on my htaccess but it won't work as root already redirects to new domain.

is there any way to solve this issue?

3
Please add your current dynamic configuration file (".htaccess") to the question so that others can point out what changes are required.arkascha

3 Answers

0
votes

Certainly what you ask is possible. Most likely you had an issue with the order of your rules if things did not work for you. You need to keep in mind that rules are processed from top to bottom. So more specialized, more detailed rules have to come first, more general rules further down.

Here is an example for the single example you gave:

RewriteEngine on

# this is a specialized rule for a specific resource
RewriteCond %{HTTP_HOST} ^olddomain\.com$
RewriteRule ^/about/?$ https://newdomain.com/who-we-are [R=301]

# this acts as a fallback for a  request not yet redirected
RewriteCond %{HTTP_HOST} ^olddomain\.com$
RewriteRule ^/?$ https://newdomain.com/ [R=301]

It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...

These rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.

And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

0
votes

You can use this :

RewriteEngine on

#redirect specific pages to specific location on new domain
RewriteCond %{HTTP_HOST} ^(www\.)?oldDomain\.com$ [NC]
RewriteRule ^/?about/?$ http://newDomain.com/who-we-are [NC,L,R=301]
#redirect the old domain root to new domain
RewriteCond %{HTTP_HOST} ^(www\.)?oldDomain\.com$ [NC]
RewriteRule ^/?$ http://newDomain.com/$1 [NC,L,R=301]

You can remove the %{HTTP_HOST} condition in both rules above if your domains (old and new) are on different servers or pointing to a different document root on the same server.

0
votes

Thanks, everyone, issue is resolved.

    RewriteEngine on
    RewriteCond %{HTTP_HOST} hitechos\.com [NC]
    RewriteCond %{REQUEST_URI} ^/$
    Rewriterule ^(.*)$ https://www.hitechdigital.com/ [L,R=301]

Regards Manoj Soni