2016-01-16 48 views
1

我試圖在主目錄的.htaccess文件中使用RewriteRule將一個子目錄重定向到另一個子目錄。htaccess如果沒有結尾的斜槓,RewriteRule不能正常工作

例如:http://website.com/subdir應重寫爲http://website.com/another_dir/destination_dir,但用戶仍應在地址欄中看到http://website.com/subdir

如果用戶使用尾部斜線結束URL,則此功能完美無缺。例如:http://website.com/subdir/(生成一個200

然而,如果省略了斜線,則產生301重定向和我們所看到的不希望的目標目錄例如,http://website/subdir將用戶重定向到http://website/another_dir/destination_dir

下面是有關部分。的.htaccess的:

# URL Rewriting: 
RewriteEngine On 
RewriteBase/

... 

# Redirect subdirectories: 
RewriteRule ^subdir(.*)$ another_dir/destination_dir$1 [PT,NC,QSA,L] 

任何援助表示讚賞

回答

0

速戰速決將

DirectorySlash OFF在這個htaccess的,並在DirectorySlash ON一個another_dir的.htaccess/

1

你可以在你的重寫規則的正則表達式匹配可選斜線。

RewriteRule ^subdir(/?.*)$ another_dir/destination_dir$1 [PT,NC,QSA,L] 

注剛過/?subdir。這表示在子目錄後面可能有或沒有斜槓,所以正則表達式將以任何方式匹配。

相關問題