重写移动url的规则

我必须将移动用户redirect到一个新的URL( /amp/... ),所以我将这个片段代码添加到我的Apache站点configuration:

 RewriteCond %{HTTP_REFERER} !^http://test.domain.com [NC] RewriteCond %{REQUEST_URI} !^/amp/.*$ RewriteCond %{HTTP_USER_AGENT} "android|blackberry|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC] RewriteRule ^(.*)$ /amp/$1 [L,R=302]` 

问题是我只在请求主页时( test.domain.com )redirect,而不是当我请求任何其他URL(例如test.domain.com/category/web )。 任何想法我做错了什么?

这是一个WordPress的网站,完整的重写模块是:

 <Directory /var/www/project> DirectoryIndex index.php Options None FollowSymLinks AllowOverride none <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^uploads/(.+) http://myCDN.com/uploads/$1 [R,L] RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] RewriteCond %{HTTP_REFERER} !^http://test.domain.com [NC] RewriteCond %{REQUEST_URI} !^/amp/.*$ RewriteCond %{HTTP_USER_AGENT} "android|blackberry|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC] RewriteRule ^(.*)$ /amp/$1 [L,R=302] </IfModule> </Directory> 

您的redirect需要在前端控制器之前 (内部重写)。 前端控制器捕获除文档根目录(主页)以外的每个请求。 因此,您的redirect只能在主页上进行处理,而对于其他所有请求则只能被忽略。

换一种说法:

 RewriteEngine On RewriteBase / RewriteCond %{HTTP_REFERER} !^http://test.domain.com [NC] RewriteCond %{REQUEST_URI} !^/amp/.*$ RewriteCond %{HTTP_USER_AGENT} "android|blackberry|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC] RewriteRule ^(.*)$ /amp/$1 [L,R=302] RewriteRule ^uploads/(.+) http://myCDN.com/uploads/$1 [R,L] RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] 

一般来说,外部redirect应该总是在内部重写之前进行。

你提到这是在你的“Apache站点configuration”。 我假设这些指令位于“站点configuration”(即在目录上下文中 )的<Directory>容器 。 (这些指令不会在服务器configuration虚拟主机上下文中工作,即直接在“站点configuration”中)。