.htacess Rewrite URL to Redirect certain request to subdmian

Viewed 34

I want to redirect to the mobile version of the website when the request is made from Ios Browser and Successfully able to do it with following rule

   RewriteCond %{HTTP_USER_AGENT} iPhone|iPod
   RewriteCond %{THE_REQUEST} !(reset) [NC]
   RewriteRule ^$ https://m.mydoamin1.com [L,R=302]

But I don't want to hard code my domain name here since my website is accessible from 2 domains

For Example

  • mydomain1.com shud redirect to m.mydomain1.com
  • mydomain2.com shud redirect to m.mydomain2.com

I'm trying to archive this with a single rule-but this doesn't work

 RewriteCond %{HTTP_USER_AGENT} iPhone|iPod
 RewriteCond %{THE_REQUEST} !(reset) [NC]
 RewriteRule ^$ https://m.{REQUEST_URI} [L,R=302]
1 Answers

This rule should work for you:

RewriteCond %{HTTP_USER_AGENT} iPhone|iPod [NC]
RewriteCond %{THE_REQUEST} !reset [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^/?$ https://m.%1 [L,R=301]

Condition RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC] will match mydomain1.com or www.mydomain1.com but will always grab mydomain1.com in capture group #1 which is used in RewriteRule as %1.

Related