Thursday, September 1, 2022

[SOLVED] Redirecting old sub domain to new Domain with alias parameter in URL

Issue

I'm successful redirecting the old sub domain to new one Here is the httpd.conf

<VirtualHost *:80>
   ServerName  abcd.com
   ServerAlias *.abcd.com
   RewriteEngine on
   Redirect 301 / http://pqr.com/
</VirtualHost>

The links like: mumbai.abcd.com/venue/320089-girgaum-chowpatty gets redirected to http://pqr.com/venue/320089-girgaum-chowpatty

But instead I would like to achieve: http://pqr.com/mumbai/320089-girgaum-chowpatty

Also just mumbai.abcd.com should redirect to http://prq.com/mumbai

I want to get the server alias and append in the url of new domain . How can I do that?


Solution

The common place to define these redirects would be an .htaccess in your web root directory instead of httpd.conf, because it is much more flexible and does not require you to restart apache.

So I suggest you remove these lines from httpd.conf:

RewriteEngine on
Redirect 301 / http://pqr.com/

And create an .htaccess file next to you root index.php with these contents:

RewriteEngine on

# Redirect mumbai.abcd.com to prq.com/mumbai
RewriteCond %{HTTP_HOST} ^mumbai\.abcd\.com$
RewriteRule ^$ http://prq.com/mumbai/ [L,R=301]

# Redirect mumbai.abcd.com/something/123456-foo to prq.com/mumbai/123456-foo
RewriteCond %{HTTP_HOST} ^mumbai\.abcd\.com$
RewriteRule ^(.+)/([^/]+)(/?)$ http://prq.com/mumbai/$2 [L,R=301]

If mumbai.abcd.com was just an example and you have more subdomains, try this:

RewriteEngine on

# Redirect *.abcd.com to prq.com/*
RewriteCond %{HTTP_HOST} ^([^/.]+)\.abcd\.com$
RewriteRule ^$ http://prq.com/%1/ [L,R=301]

# Redirect *.abcd.com/something/123456-foo to prq.com/*/123456-foo
RewriteCond %{HTTP_HOST} ^([^/.]+)\.abcd\.com$
RewriteRule ^(.+)/([^/]+)(/?)$ http://prq.com/%1/$2 [L,R=301]


Answered By - Jan Papenbrock
Answer Checked By - Mildred Charles (WPSolving Admin)