0

你能告诉我为什么这个重写没有通过第二个值吗?tt 值已传入 %1,但名称不在 %2 中

RewriteBase /
RewriteRule ^wedding-hair-and-make-up-images-([^/]*)-(.*)\.php$ franchisee-gallery.php?franchise_name=$1&id_tt=$2 [L]
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD|TRACE)\ /franchisee-gallery.php
RewriteCond %{QUERY_STRING} name=([^\&]*) 
RewriteCond %{QUERY_STRING} tt=(.*) 
RewriteRule ^franchisee-gallery.php$ wedding-hair-and-make-up-images-%1-%2.php? [R,L]  
4

1 回答 1

1

%N 指的是最后一个条件的匹配组,所以 %1​​ 是条件的匹配: RewriteCond %{QUERY_STRING} tt=(.*)

http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#RewriteCond

RewriteCond 反向引用:这些是 %N (1 <= N <= 9) 形式的反向引用,它提供从当前条件集中最后匹配的 RewriteCond 访问模式的分组部分(同样,在括号中)。

对于您的情况,您可以将“franchisee-gallery.php”的访问权限重写为另一个 PHP,从而进行重定向(通过 HTTP Header Location)。我不知道如何通过 Rewrite 做到这一点。也许使用 PERL 外部重写程序,使用 RewriteMap。


解决方案成立!使用自定义环境变量,可以将匹配的值存储在临时变量中!

RewriteEngine on
RewriteBase /
RewriteRule ^wedding-hair-and-make-up-images-([^/]*)-(.*)\.php$ franchisee-gallery.php?franchise_name=$1&id_tt=$2 [L]

RewriteCond %{REQUEST_URI} ^/franchisee-gallery.php
RewriteCond %{QUERY_STRING} name=([^&]*)
RewriteRule .* - [E=name:%1]

RewriteCond %{REQUEST_URI} ^/franchisee-gallery.php
RewriteCond %{QUERY_STRING} tt=([^&]*)
RewriteRule .* - [E=tt:%1]

RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD|TRACE)\ /franchisee-gallery.php
RewriteRule ^franchisee-gallery.php$ wedding-hair-and-make-up-images-%{ENV:name}-%{ENV:tt}.php? [R,L]
于 2012-07-12T13:41:32.407 回答