首先,我在根文件夹上创建了.htaccess。
代码应该是这样的
RewriteEngine on
# Rewrite /foo/bar to /foo/bar.php
RewriteRule ^([^.?]+)$ %{REQUEST_URI}.php [L]
# Return 404 if original request is /foo/bar.php
RewriteCond %{THE_REQUEST} "^[^ ]* .*?'.php[? ].*$"
RewriteRule .* - [L,R=404]
# NOTE! FOR APACHE ON WINDOWS: Add [NC] to RewriteCond like this:
# RewriteCond %{THE_REQUEST} "^[^ ]* .*?'.php[? ].*$" [NC]
结果如下:localhost/domain/index.php变成localhost/domain/index。这就是成功。
但问题是我想导航到子目录命名为demo在索引之后。它应该是这样的localhost/domain/index/demo/firstfolder/index
当我尝试时,它被写为"请求的URL/domain/demo/firstfolder/.php在此服务器上找不到。"。
我错过了什么?
我想要的是这样的。
localhost/domain/demo/firstfolder/.php变为localhost/domain/index/demo/subsubfolder.
任何想法?
最好不要使用%{REQUEST_URI}.php
,因为%{REQUEST_URI}
可以有尾斜杠。
让你的规则像这样:
RewriteEngine on
RewriteBase /domain/
# Return 404 if original request is /foo/bar.php
RewriteCond %{THE_REQUEST} '.php
RewriteRule ^ - [L,R=404]
# Rewrite /domain/bar to /domain/bar.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+?)/?$ $1.php [L]