的.htaccess删除尾随在URL斜线除非文件夹
问题描述:
我已经有了一个基本的URL重写导航到文件夹时除外效果很好,:的.htaccess删除尾随在URL斜线除非文件夹
当用户导航到任何文件夹mydomain.com/folder/
他被重定向到mydomain.com/folder?pl1=css
,导致无限重定向循环。
我试过在页面重定向到不带尾部斜线的规则上面添加RewriteCond %{REQUEST_FILENAME}/ -d
。这解决了无限循环的问题,而是打破了重定向到页面,而无需结尾的斜杠(我想保持对SEO的原因:http://googlewebmastercentral.blogspot.be/2010/04/to-slash-or-not-to-slash.html
我的问题:
- 如何处理文件夹correclty =>显示当导航到
mydomain.com/folder
或mydomain.com/folder/
(没有向url添加变量)时,默认页面(/folder/index.html;如果存在)在文件夹内 - 对于额外布朗尼分:如何优化第二部分的重写,如不要使用6行代码:-)
这是我的代码:
# Start the rewrite engine
<IfModule mod_rewrite.c>
Options +FollowSymlinks
Options -MultiViews
RewriteEngine On
</IfModule>
# Remove trailing slash
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Rule below fixes loop, but breaks redirection
# RewriteCond %{REQUEST_FILENAME}/ -d
# Handle my GET variables
RewriteRule ^([A-Za-z0-9-_]+)/?$ index.php?pl1=$1 [L]
RewriteRule ^([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/?$ index.php?pl1=$1&pl2=$2 [L]
RewriteRule ^([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/?$ index.php?pl1=$1&pl2=$2&pl3=$3 [L]
RewriteRule ^([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/?$ index.php?pl1=$1&pl2=$2&pl3=$3&pl4=$4 [L]
RewriteRule ^([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/?$ index.php?pl1=$1&pl2=$2&pl3=$3&pl4=$4&pl5=$5 [L]
RewriteRule ^([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/?$ index.php?pl1=$1&pl2=$2&pl3=$3&pl4=$4&pl5=$5&pl6=$6 [L]
答
如果这个“...导航到文件夹时除外...”是指现有文件夹,您可以尝试添加之前,在接下来的3行评论# Remove trailing slash
:
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
它可以减少规则的单一,只要是没有问题的空键(pl6=
)。像这样:
# Handle my GET variables
RewriteCond %{REQUEST_URI} !index\.php
RewriteRule ^([A-Za-z0-9-_]+)/?([A-Za-z0-9-_]+)?/?([A-Za-z0-9-_]+)?/?([A-Za-z0-9-_]+)?/?([A-Za-z0-9-_]+)?/?([A-Za-z0-9-_]+)?/? index.php?pl1=$1&pl2=$2&pl3=$3&pl4=$4&pl5=$5&pl6=$6 [L,NC]
使所有参数都是可选的,除了第一个参数。
非常感谢您的回答! 如果我实现了答案的第一部分,除了重定向到'/ folder /?pl1 = folder'之外,我会得到所需的效果。有任何想法吗? 您的答案的第二部分似乎不起作用,因为它会在我的系统中产生404错误。另外,我确实有空钥匙。 – maartenmachiels 2013-04-26 14:05:05
“*另外,我确实有空键*”。然后,你需要为每个参数制定一个规则,就像你的问题一样。 – 2013-04-26 14:34:53
“* ...重定向到'/文件夹/?pl1 =文件夹*”我想它会被另一个添加查询的规则重定向。你可以尝试在我的答案中删除'RewriteCond%{REQUEST_FILENAME} -f [OR]',这样它就不会查找文件。但不确定结果。确保在进行任何测试之前浏览器的缓存已清除。 – 2013-04-26 14:48:48