.htaccess RewriteRule 不适用于“/”
.htaccess RewriteRule does not work with "/"
我正在尝试将我的网页从 http://example.com/page.php
重定向到 http://example.com/page/
。我为此尝试使用的规则如下:
RewriteRule ^(.*)/([a-z0-9A-Z]+)\.(php|html)$ // [R=301,L]
虽然此正则表达式在与 (http://www.regextester.com/) 等在线正则表达式测试器一起使用时正确检测到页面,但它在我的 .htaccess 文件中未完成...
如何让这个正则表达式在 .htaccess 文件中工作?
编辑
所以现在我有这个来确保实际页面可见:
RewriteRule ^([a-z0-9A-Z]+)\.(php|html)$ // [R=301,L]
RewriteRule ^([a-z0-9A-Z]+)/$ /.php [L]
但是,Chrome 告诉我我有一个重定向循环,我可以理解。我该如何阻止它?
.htaccess
是每个目录指令,Apache 从 RewriteRule
URI 模式中去除当前目录路径(因此前导斜杠)。
您可以使用以下规则:
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=302,L,NE]
# To internally forward /dir/file to /dir/file.php
RewriteCond %{DOCUMENT_ROOT}/\.php -f [NC]
RewriteRule ^(.+?)/?$ /.php [L]
这会将 /page.php
重定向到 /page/php/
,但如果没有其他规则,您将获得 404。
我正在尝试将我的网页从 http://example.com/page.php
重定向到 http://example.com/page/
。我为此尝试使用的规则如下:
RewriteRule ^(.*)/([a-z0-9A-Z]+)\.(php|html)$ // [R=301,L]
虽然此正则表达式在与 (http://www.regextester.com/) 等在线正则表达式测试器一起使用时正确检测到页面,但它在我的 .htaccess 文件中未完成...
如何让这个正则表达式在 .htaccess 文件中工作?
编辑
所以现在我有这个来确保实际页面可见:
RewriteRule ^([a-z0-9A-Z]+)\.(php|html)$ // [R=301,L]
RewriteRule ^([a-z0-9A-Z]+)/$ /.php [L]
但是,Chrome 告诉我我有一个重定向循环,我可以理解。我该如何阻止它?
.htaccess
是每个目录指令,Apache 从 RewriteRule
URI 模式中去除当前目录路径(因此前导斜杠)。
您可以使用以下规则:
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=302,L,NE]
# To internally forward /dir/file to /dir/file.php
RewriteCond %{DOCUMENT_ROOT}/\.php -f [NC]
RewriteRule ^(.+?)/?$ /.php [L]
这会将 /page.php
重定向到 /page/php/
,但如果没有其他规则,您将获得 404。