2014-10-10 67 views
2

.htaccess代碼,的.htaccess的RewriteCond%{} REQUEST_FILENAME!-d不工作

RewriteEngine On 
RewriteBase/

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

RewriteRule ^p/([A-Za-z0-9-_]+)/?$ redirect.php?url=$1 [NC,L] # 
RewriteRule ^[A-Za-z0-9-_]+/([A-Za-z0-9-_]+)/?$ product.php?seo_id=$1 [NC,L] # 
RewriteRule ^([A-Za-z0-9-_]+)/?$ catalog.php?seo_id=$1 [NC,L] # 

我試圖訪問http://example.com/directory/這是在服務器已經存在,但我不能訪問它。沒有發生任何錯誤,但是,如果存在對已存在的目錄的請求,則不應應用RewriteCond %{REQUEST_FILENAME} !-d之後的規則。

但在這種情況下,3個規則中的一個正在申請。

RewriteCond %{REQUEST_FILENAME} !-d似乎無法正常工作。

+0

你說的不工作呢?你想要訪問哪個網址? – 2014-10-10 07:27:36

+0

請查看我編輯的答案。 – 2014-10-10 07:29:47

+0

什麼是錯誤?是使用3重寫規則之一? – 2014-10-10 07:32:37

回答

2

RewriteCond只適用於下一個RewriteRule,而不適用於所有的人。然後,您應該複製它們,就像這樣:

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^p/([A-Za-z0-9-_]+)/?$ redirect.php?url=$1 [NC,L] # 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^[A-Za-z0-9-_]+/([A-Za-z0-9-_]+)/?$ product.php?seo_id=$1 [NC,L] # 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^([A-Za-z0-9-_]+)/?$ catalog.php?seo_id=$1 [NC,L] # 

另一種解決方案包括使用S標誌(跳過)和反轉的RewriteCond:Apache doc for S flagexamples(法語文章,但htaccess的片段不需要翻譯: ))


編輯與另一種解決方案(未測試):

# if it's a file or a folder 
RewriteCond %{REQUEST_FILENAME} -d [OR] 
RewriteCond %{REQUEST_FILENAME} -f 
RewriteRule .* - [S=3] # then skip the next 3 rules 
RewriteRule ^p/([A-Za-z0-9-_]+)/?$ redirect.php?url=$1 [NC,L] 
RewriteRule ^[A-Za-z0-9-_]+/([A-Za-z0-9-_]+)/?$ product.php?seo_id=$1 [NC,L] 
RewriteRule ^([A-Za-z0-9-_]+)/?$ catalog.php?seo_id=$1 [NC,L] 
+0

是否有任何解決方案,如果我只想寫一次,而不是在所有規則之前到處寫字(到處都是蠻力) – 2014-10-10 07:44:55

+0

我用另一個解決方案編輯了我的答案 – 2014-10-10 07:49:13

相關問題