我试图按照如下所述在 Apache 中编辑 URL,但我没有成功。
如果 URI 以字母 'a' 开头且第三位不是 '7' 且总位数为 6
然后 url http://example.com/a/123456/
应该重写为 http://example.com/sixdigits.php
如果 URI 以字母“b”开头且第三位数字不是“7”且总位数为 4,则应将 url http://example.com/b/1234/
重写为http://example.com/fourdigits.php
这是我对 .htaccess 文件的尝试。
RewriteRule ^a/[0-9]+/$ sixdigits.php
RewriteRule ^b/[0-9]+/$ fourdigits.php
最佳答案
另一个正则表达式变体:
RewriteEngine On
RewriteRule ^a/\d{2}[^7\D]\d{3}/?$ sixdigits.php [L]
RewriteRule ^b/\d{2}[^7\D]\d/?$ fourdigits.php [L]
[^7\D]
将匹配除7
之外的任何数字
关于apache - 如何根据条件重写 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65553313/