2013-02-18 140 views
1

我有一个重写规则可以正常工作,其格式为site.com/dir/page1.php的任何页面都被翻译成格式为site.com/page1。我实现了与:如何为.htaccess重写规则指定一个目录?

# 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,并以某种方式我需要指定重写规则是专门为'includes'目录?但我可能是错的,任何帮助表示赞赏,谢谢!

更新:

我试图解决在这里忽略 '博客' 目录:

How to configure .htaccess file for rewrite rules to get rid of extensions, trailing slashes, www?

#skip wordpress site which starts with blog 
RewriteCond %{REQUEST_URI} !^/blog [NC] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^([^\.]+)$ $1.php [NC,L] 

但是这并没有为我遗憾的是工作。

+0

请发布博客网址的样本。是否应该在'blog /'中进行重写? – 2013-02-18 13:29:04

+0

感谢您的回复,它是一个WordPress博客,我只是试图访问博客“正常/开箱即用”,即site.com/blog,所以在我的理解中,我只需要说一些说全部离开/单独的博客链接(这也包括/ blog/wp-admin等)。谢谢。 – user1063287 2013-02-18 13:31:52

回答

1

为了防止重写的blog/,将您的规则5,6(这是generi重写)之前如下:

# 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 
+0

非常感谢,第一个代码组合示例正在工作 - 谢谢! – user1063287 2013-02-18 13:50:33