Search code examples
.htaccesshttp-redirectmod-alias

Redirect 301 With Matching Parameter .htaccess


I am redirecting a website from old traditional (m.) url to responsive design url .

Here is my example of the url and htaccess.

Old traditional URL => New Responsive design URL 

m.mydomain.com => mydomain.com
m.mydomain.com/mobiles.html => mydomain.com/mobiles
m.mydomain.com/mobiles/android-phones.html => mydomain.com/mobiles/android-phones-price.html
m.mydomain.com/mobiles/android/samsung-phones.html => mydomain.com/mobiles/samsung/android-phones.html

.htaccess

RedirectMatch 301  m.mydomain.com/mobiles/android/(.*)-phones.html mydomain.com/mobiles/$1/android-phones.html

But i am getting Internal Server Error (500) . How to Fix this issue.


Solution

  • You cannot match host name using RedirectMatch directive. Use mod_rewrite rules instead:

    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} ^m\.mydomain\.com$ [NC]
    RewriteRule ^$ http://mydomain.com/ [L,R=301]
    
    RewriteCond %{HTTP_HOST} ^m\.mydomain\.com$ [NC]
    RewriteRule ^mobiles\.html$ http://mydomain.com/mobiles [L,NC,R=301]
    
    RewriteCond %{HTTP_HOST} ^m\.mydomain\.com$ [NC]
    RewriteRule ^mobiles/android-phones\.html$ http://mydomain.com/mobiles/android-phones-price.html [L,NC,R=301]
    
    RewriteCond %{HTTP_HOST} ^m\.mydomain\.com$ [NC]
    RewriteRule ^mobiles/android/samsung-phones\.html$ http://mydomain.com/mobiles/samsung/android-phones.html [L,NC,R=301]