【问题标题】:htaccess - issue when trying to redirecting to mobile website (on subdomain)htaccess - 尝试重定向到移动网站时出现问题(在子域上)
【发布时间】:2017-01-27 06:16:29
【问题描述】:

我的一个网站上的 .htaccess 文件有一些问题。

我得到的错误是我尝试重定向太多次。

它目前的工作方式是每次我在桌面上时都需要将 URL 重定向到子文件夹 /dist,如果我在移动设备上,我需要转到网站的移动版本而不是是以mobile.开头的子域

我的 htaccess 文件如下所示:

RewriteEngine on
RewriteBase /

# Check if this is the noredirect query string
RewriteCond %{QUERY_STRING} (^|&)m=0(&|$)

# Set a cookie, and skip the next rule
RewriteRule ^ - [CO=mredir:0:www.mywebsite.com/dist]

RewriteCond %{HTTP_HOST} ^mywebsite\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.mywebsite\.com$
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^/?$ "http\:\/\/www\.mywebsite\.com\/dist" [R=301,L]

# Check if we're not already on the mobile site
RewriteCond %{HTTP_HOST}          !^m\.

# Can not read and write cookie in the same request, must duplicate condition
RewriteCond %{QUERY_STRING} !(^|&)m=0(&|$)

# Check to make sure we haven't set the cookie before
RewriteCond %{HTTP_COOKIE}        !^.*mredir=0.*$ [NC]

# Now redirect to the mobile site
RewriteRule ^ http://mobile.mywebsite.com [R,L]

有人可以帮忙吗?

这是我第一次尝试这样的事情

谢谢

【问题讨论】:

  • 如果您也发布错误消息也会有所帮助。
  • 加载资源失败:net::ERR_TOO_MANY_REDIRECTS
  • 您的移动网站是否以m.mobile. 开头?
  • 我已经设置了两个 m。和移动设备。
  • 试试你的条件RewriteCond %{HTTP_HOST} !^(m|mobile)\. [NC]`

标签: .htaccess redirect mod-rewrite


【解决方案1】:

你可以这样:

RewriteEngine on
RewriteBase /

# Check if this is the noredirect query string
RewriteCond %{QUERY_STRING} (^|&)m=0(&|$)    
# Set a cookie, and skip the next rule
RewriteRule ^ - [CO=mredir:0:www.mywebsite.com/dist]

RewriteCond %{HTTP_HOST} ^(?:www\.)?mywebsite\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^/?$ http://www.mywebsite.com/dist [R=301,L]

# Check if we're not already on the mobile site
RewriteCond %{HTTP_HOST} !^(m|mobile)\. [NC]    
# Can not read and write cookie in the same request, must duplicate condition
RewriteCond %{QUERY_STRING} !(^|&)m=0(&|$) [NC]    
# Check to make sure we haven't set the cookie before
RewriteCond %{HTTP_COOKIE} !^.*mredir=0 [NC]
# check mobile agents
RewriteCond %{HTTP_ACCEPT} "text\/vnd\.wap\.wml|application\/vnd\.wap\.xhtml\+xml" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "sony|symbian|nokia|samsung|mobile|windows ce|epoc|opera" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "mini|nitro|j2me|midp-|cldc-|netfront|mot|up\.browser|up\.link|audiovox" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "blackberry|ericsson,|panasonic|philips|sanyo|sharp|sie-" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "portalmmm|blazer|avantgo|danger|palm|series60|palmsource|pocketpc" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "smartphone|rover|ipaq|au-mic,|alcatel|ericy|vodafone\/|wap1\.|wap2\.|iPhone|android" [NC] 
# Now redirect to the mobile site
RewriteRule ^ http://mobile.mywebsite.com [R=302,NE,L]

【讨论】:

  • 不,它每次都会重定向到移动版本,即使我在桌面上也是如此
  • 为此,您需要检查用户代理,并且您的问题中不存在该检查。
  • 好吧,根据这个答案,检查应该适用于移动设备:stackoverflow.com/questions/3680463/… 知道为什么它不适用于我的情况吗?感谢您到目前为止的帮助,顺便说一句:)
  • 但答案是使用%{HTTP:x-wap-profile}%{HTTP:Profile} 标头来检测移动设备。你可以试试这个,而不是我上面建议的HTTP_USER_AGENT 条件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-02
  • 1970-01-01
  • 2017-01-28
  • 2015-07-24
  • 2011-05-30
  • 2013-06-04
相关资源
最近更新 更多