Using HTTP_HOST to identify the identify the previ

2019-07-24 14:29发布

问题:

I have changed my Domain name of my site test.com to test2.com

Domain name changed from test.com to test2.com

I want to know if all the pages like test.com/* will be redirected to test2.com/*

In test2.com, I am writing the rewrite rule in mod_rewrite to identify the smart mobile device.

For all smart phone I would like to redirect requests test.com/* to test2.com (home page)

For every non smart phone I would like to redirect test.com/* to test2.com/*

I have researched the following condition for this. I am not sure if this is correct or not.

I have tried some thing like this.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^test\.com$ [AND]
RewriteCond %{HTTP_USER_AGENT} mobile
RewriteCond %{HTTP_USER_AGENT} !(android|blackberry|iphone|ipod|windows phone) [NC]
RewriteRule ^(.*)$ http://test2.com/mobil/#1 [L]

回答1:

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your DOCUMENT_ROOT/.htaccess file:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# For mobile devices:
RewriteCond %{HTTP_HOST} ^(www\.)?test\.com$ [NC]
RewriteCond %{HTTP_USER_AGENT} (android|blackberry|iphone|ipod|windows phone) [NC]
RewriteRule ^ http://test2.com/ [L,R=301]

# For non-mobile devices:
RewriteCond %{HTTP_HOST} ^(www\.)?test\.com$ [NC]
RewriteRule ^(.*)$ http://test2.com/$1 [L,R=301]