如何在省略尾部斜杠时停止URL重定向?

时间:2015-02-02 05:23:00

标签: .htaccess

我写了htaccess重写规则来从另一个文件夹中获取内容。例如:如果URL为http://test.com/folder2/folder3/,则从folder1/folder2/folder3目录加载索引文件。在这种情况下,一切都运行良好,但如果我省略URL中的尾部斜杠,即如果URL为http://test.com/folder2/folder3,则URL将重定向到http://test.com/folder1/folder2/folder3/。如何在不更改URL的情况下从folder1/folder2/folder3加载内容?

重写规则是:

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(folder2/.*)$ folder1/$1 [L]

2 个答案:

答案 0 :(得分:1)

这是因为mod_dir模块在​​mod_dir之后运行并在重写的URI上添加了一个斜杠。您可以将htaccess更改为:

Options +FollowSymLinks
DirectoryIndex index.php

RewriteEngine on
RewriteBase /

# add trailing slash to directories
RewriteCond %{DOCUMENT_ROOT}/folder1/$1 -d
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=302,NE]

RewriteRule ^(folder2/.*)$ folder1/$1 [L,NC]

答案 1 :(得分:0)

我通过强制对每个网址进行尾部删减来找到解决问题的方法。因此,.htaccess文件是:

Options +FollowSymLinks
RewriteEngine on

# Force Trailing Slash
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]

RewriteRule ^(folder2/.*)$ folder1/$1 [L]