【发布时间】:2013-04-26 10:17:25
【问题描述】:
除了导航到文件夹:
当用户导航到任何文件夹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
我的问题:
- 如何正确处理文件夹 => 导航到
mydomain.com/folder或mydomain.com/folder/时在文件夹内显示默认页面(/folder/index.html;如果存在)(不向 url 附加变量) - 额外加分:如何优化重写的第二部分,以免使用 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]
【问题讨论】:
标签: apache .htaccess mod-rewrite