2016-01-16 16 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。这表示在子目录后面可能有或没有斜杠,所以正则表达式将以任何方式匹配。

相关问题