Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

We have a mobile site that is served using multiple domains. We want to redirect users who are using desktop user agents to our desktop sites while preserving the original domain the user used to access the site.

There seem to be a few .htaccess variables that can provide the domain but they include the subdomain as well. My current code is:

RewriteCond %{HTTP_HOST} ^m\. [NC]  
RewriteCond %{HTTP_USER_AGENT} !(android|blackberry|googlebot-mobile|iemobile|ipad|iphone|ipod|opera\smobile|palmos|webos) [NC]  
RewriteRule ^.*$ http://www.example.com/%{REQUEST_URI}/ [R=301,L]

This works fine, but we want to be able to adjust the domain to be the domain the request rather using than a static domain (example.com).

Is there a way to accomplish this with .htaccess? Thanks!

share|improve this question

migrated from superuser.com Apr 20 at 0:31

This question came from our site for computer enthusiasts and power users.

1 Answer 1

Yes, you can use RewriteCond to match any part of the domain you want in %{HTTP_HOST} and then reference it in your RewriteRule by using a %1 backreference (%# refers to matched items in parenthesis in the most-recently matched RewriteCond directive).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.