【问题标题】:.htaccess remove trailing slash in url except when folder.htaccess 删除 url 中的尾部斜杠,文件夹时除外
【发布时间】: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/foldermydomain.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


    【解决方案1】:

    如果此“...除非导航到文件夹...”表示现有文件夹,您可以尝试在注释@987654321之前添加接下来的3行@:

    RewriteCond %{REQUEST_FILENAME} -f  [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule .*   - [L]
    

    可以将规则减少到一个,只要有空的 keys (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 错误。另外,我确实介意有空键。
    • "另外,我确实介意有空键"。然后,您需要为每个参数制定一个规则,就像您的问题一样。
    • "...redirects to ´/folder/?pl1=folder" 我猜它是被另一个添加查询的规则重定向的。您可以尝试在我的回答中删除RewriteCond %{REQUEST_FILENAME} -f [OR],这样它就不会查找文件。但不确定结果。确保在进行任何测试之前清除浏览器的缓存。
    • 赢家通吃!你解决了我的问题。感谢您以如此清晰和有用的方式回答我问题的各个方面!清除缓存 aswel 的好电话。
    • 当然可以。我想我会保留我不酷的 6 行代码来重写我的变量;-)
    猜你喜欢
    • 2015-01-15
    • 1970-01-01
    • 2015-05-09
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多