2016-09-13 46 views
1

我已經在PHP中開發了自己的CMS,現在我想讓它對搜索引擎友好,但我遇到了一些問題。
htaccess和CMS的URL問題

大多數網站的頁面都可以訪問從ID,如:

www.example.com/?id=alphanumericId123 
www.example.com/?id=main 

等等,我想改寫這個頁面的URL:

www.example.com/aplhanumericId/ 
www.example.com/main/ 

我已經寫了這個重寫規則,並能正常工作:

RewriteEngine On 
RewriteRule ^([^/d]+)/?$ index.php?id=$1 [QSA] 

,但它不能爲壽工作有幾頁可以通過靜態文件訪問,或者僅僅是腳本。
難道沒有辦法做出一些例外嗎?像:

if the requested file exsist { 
    if it is protected { 
     display error 403 
    } else { 
     display file 
} } else { 
    rewrite rule 
} 

回答

1

你不應該重寫路徑現有的文件和目錄:

RewriteEngine On 
# Exclude existing files 
RewriteCond %{REQUEST_FILENAME} !-f 
# Exclude existing directories 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^([^/d]+)/?$ index.php?id=$1 [QSA] 

另外請注意,您的規則不會讓你的字母數字的ID爲你排除號碼。

替代方案是:

... 
# No forward slash 
RewriteRule ^([^\/]+)/?$ index.php?id=$1 [QSA] 

... 
# Only a selection of characters including - and _ 
RewriteRule ^([-\w]+)/?$ index.php?id=$1 [QSA] 
+1

@yessure而當你註釋掉這2個條件和規則是否可行? – jeroen

+1

不,我只是犯了一個錯誤,對不起。您所寫的第一個條件完美無缺,謝謝。 –

0

你重寫規則之前,請使用以下

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 

基本上,它會檢查,如果它不是一個文件,而不是一個目錄。