[英]Apache - .htaccess regex substitution in RewriteCond
要将localhost:8888/dev/test.php
替换为localhost:8888/dev/test/
,这是我创建的.htaccess
RewriteEngine on
RewriteRule ^/?(.*)/test/ /$1/test.php [NC,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /dev/test\.php [NC]
RewriteRule ^/?(.*)/test.php$ /$1/test/ [L,R=301]
我试图用/$1/
代替RewriteCond %{THE_REQUEST} ^[AZ]{3,9}\\ /dev/test\\.php [NC]
/dev/
以从rewriteRule中获取值,但这似乎没有可以正常工作,因为URL仍为http://localhost:8888/dev/test.php
而不是localhost:8888/dev/test/
在RewriteCond中替换
/dev/
所需的正则表达式的格式应该是什么?
RewriteRule ^/?(.*)/test/ /$1/test.php [NC,L] RewriteCond %{THE_REQUEST} ^[AZ]{3,9}\\ /dev/test\\.php [NC] RewriteRule ^/?(.*)/test.php$ /$1/test/ [L,R=301]
这些规则应该颠倒。 外部重定向应该在 内部重写 之前 。 否则,当重写过程重新开始时,它将被重写回/dev/test.php
(第一个指令)。
例如:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /dev/test\.php [NC]
RewriteRule ^(.*)/test\.php$ /$1/test/ [L,R=301]
RewriteRule ^(.*)/test/ /$1/test.php [NC,L]
在每个目录.htaccess
文件中, /?
模式开始时没有必要。
对THE_REQUEST
的检查只是为了避免重定向循环。 相反,您可以检查REDIRECT_STATUS
环境变量,该变量在初始请求中为空:
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.*)/test\.php$ /$1/test/ [L,R=301]
更新:
我想用
^[AZ]{3,9}\\ /dev/test\\.php
^[AZ]{3,9}\\ /$1/test\\.php
由于您在RewriteRule
模式中使用了一个万能通配符,因此这种事情似乎不是必需的? 只需在CondPattern中使其成为万能的通配符? 例如:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*/test\.php\ HTTP [NC]
我还要问这里是否需要NC
? 您是否特别需要允许TEST.PHP
等?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.