2013-02-18 27 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