【问题标题】:htaccess reg ex to add trailing slash to directories but not some drupal aliaseshtaccess reg ex 将斜杠添加到目录但不添加一些drupal别名
【发布时间】:2012-08-08 02:11:10
【问题描述】:

我对 reg ex 非常陌生,并且有许多 drupal 别名,包括出于 SEO 原因我想保留的一些遗留 .html 文件(别名)。我需要在虚拟目录上强制使用斜线,并且在 .html 别名上强制不使用斜线。

到目前为止,我有这段代码将斜杠添加到目录而不是 .html,但它仍然允许用户或代理转到 .html/ - 非常感谢重写此斜杠的删除或 404 的任何帮助。

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#is not an htm/l file
RewriteCond %{REQUEST_URI} !\.html?$ [NC]
#does not end with a slash
RewriteCond %{REQUEST_URI} !/$ 
RewriteRule ^(.+)$ http://www.example.com/$1/ [L,R=301]

【问题讨论】:

  • 这里有什么问题?当 URL 以 .html 结尾时,这些规则是否添加了斜杠?或者您是否想阻止人们在请求 html 文件时实际添加斜杠?
  • 该规则按预期工作,但我们想添加一些东西来防止人们,更重要的是蜘蛛,将尾部斜杠添加到对 html 文件的请求。据我了解,默认情况下这通常会导致 404 错误,但由于某种原因,当 .html 文件是 drupal 中的别名时,它仍会在添加斜杠时显示页面。 news.html 与 news.html/ 相同

标签: regex .htaccess drupal mod-rewrite


【解决方案1】:

尝试将其添加到RewriteBase 的正下方:

RewriteRule ^(.*)(\.html?)/$ /$1$2 [R=301,L]

【讨论】:

    猜你喜欢
    • 2016-08-24
    • 2012-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-03
    • 2015-09-09
    • 2012-03-25
    • 1970-01-01
    相关资源
    最近更新 更多