开发者

Mobile Redirect = Mod_Rewrite endless loop, what's wrong?

Hey all, I'm trying to redirect to a mobi开发者_如何学运维le site that lives in my subdirectory m, To make matters worse I was trying to only do the redirect on the index page, and then only if a cookie (mobile) isn't set (so that I can send users back to the desktop site) Anyhow, my mod-rewrite gets stuck in an endless loop here's the code from .htaccess:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/m/.*$
RewriteCond %{HTTP_COOKIE} !^.*mobile.*$ [NC]
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 mini|IEmobile|DoCoMo" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "nitro|j2me|midp-|cldc-|netfront|mot|up\.browser|up\.link|audiovox"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "blackberry|ericsson,|panasonic|philips|sanyo|sharp|sie-|ipad|ipod|iphone"[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\.|mobile safari|android"[NC]
RewriteRule ^(.*)$ m/$1 [R=307,NC,L,QSA]

What's making this get stuck?

also, it adds /var/www/html//var/www/html//var/www/html//var/www/html/ to the url


I see two problems right off the bat.

  1. The constant rewriting to /var/www/html... is probably happening because you are not setting RewriteBase correctly. It sounds like you need RewriteBase /var/www/html, presuming that is in fact the root directory of your site.
  2. If I recall correctly, in per-directory rewrites (that is, rewrites in .htaccess), you do not ever have a leading slash in your path. This is a problem on your first RewriteCond, in which you are checking for a leading slash before m/, which likely will never be there.

I'm not saying this will fix all your problems, and I don't have time at the moment to look further, but perhaps this will get you started.


usesIf you use Apache Mobile Filter is easier, for example to the same think you ask:

#Configuration AMF Filter
#
PerlSetEnv AMFMobileHome /usr/local/AMF
PerlSetEnv AMFProductionMode true
PerlSetEnv ServerMemCached localhost:11211
PerlTransHandler +Apache2::AMFLiteDetectionFilter
RewriteEngine on
RewriteCond %{**ENV:AMF_DEVICE_IS_MOBILE**} ^true*
RewriteRule ^(.*)$ http://m.foo.org [R=301,L] 

This is example uses AMFLiteDectionFilter module that not device repository required, but AMF supported several Device Repository such as 51Degrees.mobi, DetectRight and WURFL


How about turning on the log and see what's really going on?

RewriteLog "/var/log/apache2/rewrite.log"
RewriteLogLevel 3

Here's the documentation for the RewriteLog and RewriteLogLevel directivies:

  • http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#RewriteLog
  • http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#RewriteLogLevel


I'm not sure how or why, but the following code is working properly:

RewriteCond %{REQUEST_URI} !^/m/.*$
RewriteCond %{HTTP_COOKIE} !^.*mobile.*$ [NC]
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 mini|IEmobile|DoCoMo" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "nitro|j2me|midp-|cldc-|netfront|mot|up\.browser|up\.link|audiovox"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "blackberry|ericsson,|panasonic|philips|sanyo|sharp|sie-|ipad|ipod|iphone"[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\.|mobile safari|android"[NC]
RewriteRule ^(.*)$ /m/ [L,R=307,QSA]

My best guess is that the RewriteBase was already defined in httpd.conf and causing a conflict of some sort

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜