Apache - RewriteCond中的.htaccess正则表达式替换

时间:2022-09-17 07:56:54

To replace localhost:8888/dev/test.php to localhost:8888/dev/test/, this was the .htaccess I created

要将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]

I tried to substitute /dev/ in RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /dev/test\.php [NC] with /$1/ to get the value from the rewriteRule but this doesn't seem to work as the URL remains as http://localhost:8888/dev/test.php instead of localhost:8888/dev/test/

我尝试用/ $ 1 /替换RewriteCond%{THE_REQUEST} ^ [AZ] {3,9} \ /dev/test\.php [NC]中的/ dev /来从rewriteRule中获取值但是这似乎没有工作,因为URL保持为http:// localhost:8888 / dev / test.php而不是localhost:8888 / dev / test /

What should be the format of the regex needed to substitute /dev/ in RewriteCond?

在RewriteCond中替换/ dev /所需的正则表达式的格式应该是什么?

1 个解决方案

#1


2  

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]

These rules should be reversed. The external redirect should be before the internal rewrite. Otherwise, when the rewriting process starts over, it is rewritten back to /dev/test.php (the first directive).

应该颠倒这些规则。外部重定向应该在内部重写之前。否则,当重写过程重新开始时,它将被重写回/dev/test.php(第一个指令)。

For example:

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]

In per-directory .htaccess files the /? at the start of the pattern is not necessary.

在每个目录.htaccess文件中/?在模式的开始是没有必要的。

The check against THE_REQUEST is simply to avoid a redirect loop. Instead, you can check the REDIRECT_STATUS environment variable, which is empty on the initial request:

对THE_REQUEST的检查只是为了避免重定向循环。相反,您可以检查REDIRECT_STATUS环境变量,该变量在初始请求中为空:

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.*)/test\.php$ /$1/test/ [L,R=301]

UPDATE:

I want to substitute ^[A-Z]{3,9}\ /dev/test\.php with something like ^[A-Z]{3,9}\ /$1/test\.php

我想用^ [A-Z] {3,9} \ /$1/test\.php替换^ [A-Z] {3,9} \ /dev/test\.php

Since you are using a catch-all wildcard in the RewriteRule pattern, this sort of thing does not seem to be necessary? Just make it a catch-all wildcard in the CondPattern? For example:

由于你在RewriteRule模式中使用了一个全能通配符,这种事情似乎没有必要吗?只是让它成为CondPattern的全能通配符?例如:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*/test\.php\ HTTP [NC]

I'd also question whether the NC was necessary here? Do you specifically need to allow TEST.PHP, etc.?

我还怀疑NC是否有必要在这里?你是否特别需要允许TEST.PHP等?

#1


2  

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]

These rules should be reversed. The external redirect should be before the internal rewrite. Otherwise, when the rewriting process starts over, it is rewritten back to /dev/test.php (the first directive).

应该颠倒这些规则。外部重定向应该在内部重写之前。否则,当重写过程重新开始时,它将被重写回/dev/test.php(第一个指令)。

For example:

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]

In per-directory .htaccess files the /? at the start of the pattern is not necessary.

在每个目录.htaccess文件中/?在模式的开始是没有必要的。

The check against THE_REQUEST is simply to avoid a redirect loop. Instead, you can check the REDIRECT_STATUS environment variable, which is empty on the initial request:

对THE_REQUEST的检查只是为了避免重定向循环。相反,您可以检查REDIRECT_STATUS环境变量,该变量在初始请求中为空:

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.*)/test\.php$ /$1/test/ [L,R=301]

UPDATE:

I want to substitute ^[A-Z]{3,9}\ /dev/test\.php with something like ^[A-Z]{3,9}\ /$1/test\.php

我想用^ [A-Z] {3,9} \ /$1/test\.php替换^ [A-Z] {3,9} \ /dev/test\.php

Since you are using a catch-all wildcard in the RewriteRule pattern, this sort of thing does not seem to be necessary? Just make it a catch-all wildcard in the CondPattern? For example:

由于你在RewriteRule模式中使用了一个全能通配符,这种事情似乎没有必要吗?只是让它成为CondPattern的全能通配符?例如:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*/test\.php\ HTTP [NC]

I'd also question whether the NC was necessary here? Do you specifically need to allow TEST.PHP, etc.?

我还怀疑NC是否有必要在这里?你是否特别需要允许TEST.PHP等?