.htaccess重写规则意外返回404

fhity93d  于 2022-11-16  发布在  其他
关注(0)|答案(1)|浏览(111)

使用这个htaccess文件可以进行重写,但是由于某种原因,我创建的这个新规则似乎不起作用。
RewriteRule ^(.*)/(webapps|gamedev|digitalart)$ $1/index.php [NC,PT,L]
下面是我的全部重写规则:

RewriteEngine on

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteRule ^(.*)/(webapps|gamedev|digitalart)$ $1/index.php [NC,PT,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [L]

包含此.htaccess的公共URL如下所示:
https://dustinhendricks.com/test
我正在测试的网址在这里:
https://dustinhendricks.com/test/gamedev
我希望上面的URL导航到这里(不更改浏览器中的URL):
https://dustinhendricks.com/test/index.php
相反,它返回了一个404错误。对于为什么重写没有按预期工作有什么想法吗?在这里测试过,但在这个站点htaccess tester中似乎重写得很好

8yoxcaq7

8yoxcaq71#

使用以下方法访问.htaccess:

RewriteEngine on

RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

# add .php if there is a matching .php file
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

# rewrite these paths to index.php
RewriteRule ^(webapps|gamedev|digitalart)/?$ index.php [NC,L]

请注意,您的.htaccess已经在test/子目录中,因此RewriteRule中的匹配模式将相对于test/子目录。

相关问题