我有一个重写规则工作正常,其中格式中的任何页面site.com/dir/page1.php
都被转换为格式site.com/page1
。我通过以下方式实现了这一目标:
# 1. turn on the rewriting engine
RewriteEngine On
# 2. remove the www prefix
RewriteCond %{HTTP_HOST} ^www\.site\.com [NC]
RewriteRule ^(.*)$ http://site.com/$1 [L,R=301]
# 3. send all non-secure pages to secure pages
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
# 4. if 404 not found error, go to homepage
ErrorDocument 404 /index.php
# 5. if url received with trailing slash (e.g. http://domain/test/) then go to correct page (e.g. http://domain/pages/test.php)
RewriteRule ^([^.]+)/$ includes/$1.php
# 6. if url received WITHOUT trailing slash (e.g. http://domain/test) then go to correct page (e.g. http://domain/pages/test.php)
RewriteRule ^([^.]+)$ includes/$1.php
我现在已经建立了一个博客,site.com/blog/
当我尝试访问该博客时,它正在显示主页(即 404 错误)。
我认为错误出现在第 5 部分和第 6 部分,并且我需要以某种方式指定重写规则仅适用于“包含”目录?但我可能是错的,任何帮助表示赞赏,谢谢!
更新:
我在这里尝试了忽略“博客”目录的解决方案:
如何为重写规则配置 .htaccess 文件以消除扩展名、斜杠、www?
#skip wordpress site which starts with blog
RewriteCond %{REQUEST_URI} !^/blog [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
但不幸的是,这对我不起作用。