开发者

mobile site redirect to desktop version using .htaccess

开发者 https://www.devze.com 2023-02-27 04:35 出处:网络
I have a mobile site and it has just started a service for desktop users also. How do I use .htaccess to redirect users using the desktop to the desktop version of the site?

I have a mobile site and it has just started a service for desktop users also. How do I use .htaccess to redirect users using the desktop to the desktop version of the site?

I have tried simple PHP scripts to redirect to the home page of the desktop version, but I would like the redirect to be slightly more efficient. For exam开发者_Go百科ple, I would like to redirect http://site.com/subfolder/ to http://site.com/desktop/subfolder/, and I figured that using .htaccess might be able to get the job done.

I have tried the following and it doesn't seem to work.

RewriteCond %{REQUEST_URI} !^/desktop/.*$
RewriteCond %{HTTP_USER_AGENT} "!android|!blackberry|!ipad|!iphone|!ipod|!iemobile|!webos|!googlebot-mobile" [NC]
RewriteRule ^(.*)$ /desktop/ [L,R=302]

Please help!


Try these rules in your .htaccess file:

RewriteEngine on
Options +FollowSymlinks -MultiViews
RewriteCond %{REQUEST_URI} !^/desktop/ [NC]
RewriteCond %{HTTP_USER_AGENT} ^.*(MSIE.*Windows\ NT|Lynx|Safari|Opera|Firefox|Konqueror) [NC]
RewriteCond %{HTTP_USER_AGENT} !(^.*(Opera\ Mini|SymbianOS|Mobile)) [NC]
RewriteRule ^(.*)$ /desktop/$1 [L,R=302,NC]

Update (combined with wordpress rules)

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
Options +FollowSymlinks -MultiViews

RewriteCond %{THE_REQUEST} !^GET\s/wp-login\.php [NC]
RewriteCond %{REQUEST_URI} !^/(desktop/|wp-admin/|wp-login\.php) [NC]
RewriteCond %{HTTP_USER_AGENT} ^.*(MSIE.*Windows\ NT|Lynx|Safari|Opera|Firefox|Konqueror) [NC]
RewriteCond %{HTTP_USER_AGENT} !(^.*(Opera\ Mini|SymbianOS|Mobile)) [NC]
RewriteRule ^(.*)$ /desktop/$1 [L,R=302,NC]

RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(desktop/|wp-admin/|wp-login\.php) [NC]
RewriteRule . /index.php [L]

</IfModule>
# END WordPress

R=302 will redirect with https status 302

L will make last rule

NE is for no escaping query string

$1 is your REQUEST_URI


I think the code below is target more accuracy at the desktop browsers. You can remove the 1st USER_AGENT line if you don't want to exclude IE 10 on touch based devices.

RewriteCond %{REQUEST_URI} !^/desktop/.*$
RewriteCond %{HTTP_USER_AGENT} !Windows\ NT.+Touch [NC]
RewriteCond %{HTTP_USER_AGENT} Windows\ NT\ 6|Macintosh|Ubuntu|Linux\ (x86_64|i686)|CrOS [NC]
RewriteRule ^(.*)$ /desktop/ [L,R]

All desktop browsers which are using Windows, Mac, Linux will be redirect to /desktop/.


Were you wanting to redir to the www version of your site? Then change the

RewriteCond %{REQUEST_URI} !^/(desktop/|wp-admin/|wp-login\.php) [NC]

to the following: (replace the www.example.com with your desktop site).

RewriteRule ^ http://www.example.com%{REQUEST_URI} [L,R=302] 

regards

seemore

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号