1

我有一个重写规则工作正常,其中格式中的任何页面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]

但不幸的是,这对我不起作用。

4

1 回答 1

2

为防止在 上重写blog/,请将以下内容放在您的规则 5,6 (一般重写)之前:

# Do not apply the following to the blog
RewriteCond %{REQUEST_URI} !^/blog

# 5. if url received with trailing slash (e.g. http://domain/test/) then go to correct page (e.g. http://domain/pages/test.php)
# Merged 5 & 6 together by making the trailing slash optional /?
RewriteRule ^([^.]+)/?$ includes/$1.php

另一种可能性是防止重写任何存在的目录:

# Do not apply the following to actual existing files and directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Merged 5 & 6 together by making the trailing slash optional /?
RewriteRule ^([^.]+)/?$ includes/$1.php
于 2013-02-18T13:35:22.307 回答