Apache-.htaccess regex在RewriteCond中替换
要将Apache-.htaccess regex在RewriteCond中替换,regex,apache,.htaccess,mod-rewrite,Regex,Apache,.htaccess,Mod Rewrite,要将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] 我试
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}^[A-Z]{3,9}\/dev/test\.php[NC]
中的/dev/
,以从重写规则中获取值,但这似乎不起作用,因为URL仍然是http://localhost:8888/dev/test.php
而不是localhost:8888/dev/test/
RewriteCond中替换/dev/
所需的正则表达式的格式应该是什么
这些规则应该颠倒过来。外部重定向应在内部重写之前进行。否则,当重写过程重新开始时,它将被重写回/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]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*/test\.php\ HTTP [NC]
在每个目录.htaccess
文件中,模式开头的/?
不是必需的
对请求的检查只是为了避免重定向循环。相反,您可以检查REDIRECT\u STATUS
环境变量,该变量在初始请求时为空:
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.*)/test\.php$ /$1/test/ [L,R=301]
更新:
我想用^[A-Z]{3,9}\/dev/test\.php
之类的东西替换^[A-Z]{3,9}\/$1/test\.php
由于您在重写规则
模式中使用了一个catch-all通配符,所以这种事情似乎没有必要?就让它成为CondPattern中的通配符?例如:
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]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*/test\.php\ HTTP [NC]
我还想问,这里是否需要
NC
?您特别需要允许TEST.PHP
等吗?实际发生了什么?错误?没有什么当您请求/dev/test/
时,请求是否正确路由?@w3dk url保持为localhost:8888/dev/test.php
,但是/dev/test/
呢?这条路线正确吗?(这是第一个指令正在处理的内容。)@w3dk是的,这很好,我提供的代码生成URL。只有替换会导致问题这不是我要问的,我想用^[A-Z]{3,9}\/dev/test\.php
之类的东西替换^[A-Z]{3,9}\/$1/test\.php
。不确定为什么需要这样做?我更新了我的答案。哦,我对重写规则感到困惑,认为也需要替换,这确实是毫无意义的。。。哈哈,谢谢,应该是这样的。但是请求状态似乎不起作用。很抱歉,这应该是重定向状态
。