我没有检查你重写规则的有效性或逻辑。但根据你的要求,我认为你缺少的是
RewriteBase
指令。
rewritebase指令指定要用于
每个目录(htaccess)重写规则指令
相对路径。
您可以这样修改.htaccess文件。
您的主目录:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteRule ^(.+)\.php$ $1 [R,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,END]
Options -Indexes
ErrorDocument 404 domain.com/page-not-found
您的子目录:
RewriteEngine On
RewriteBase /subdirectory/
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteRule ^(.+)\.php$ $1 [R,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,END]
Options -Indexes
ErrorDocument 404 domain.com/page-not-found
这里,唯一的区别是我加了
RewriteBase
并更改了重定向位置中的绝对路径。(
$1
而不是
/$1
)
我希望有帮助。如果您有任何疑问,请随时询问:)