2011-12-09 31 views
0

我有一種情況,在/ products目錄中有一堆舊內容。展望未來,新內容將進入/產品文件夾。如果文件或目錄不存在,我想要做的是重定向到/ product。.htaccess如果文件或二級目錄不存在,則重定向

我有一個.htaccess文件在/產品文件夾中的以下內容:

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ /product/$1 [R=301,L] 

這非常適用於在/產品目錄中的文件和包含的index.htm文件或其他文件名的子目錄,但我想,以限制對一個不存在的目錄重定向是2級:

/產品/ someproduct - 不重定向

/產品/ someprodu ct/otherproduct 如果不存在,重定向到/ product/someproduct/anotherproduct

如何強制規則僅適用於2級或更深的子目錄?

回答

0

請嘗試以下

RewriteEngine On 
RewriteBase/

#for all /products/somefolder/any_folder_level_deeep content 
RewriteCond %{REQUEST_URI} ^/products/([^/]+/.+)$ [NC] 
#if file does not exist 
RewriteCond %{REQUEST_FILENAME} !-f 
#and directory does not exist 
RewriteCond %{REQUEST_FILENAME} !-d 
#redirect to product folder 
RewriteRule . /product/%1 [R=301,L] 
相關問題