【问题标题】:Removing trailing slash and redirecting html删除尾部斜杠并重定向 html
【发布时间】:2012-07-01 13:04:35
【问题描述】:

我遇到的问题是第一个 URL 有效,而第二个无效。

http://www.example.com/podcast/episode1
http://www.example.com/podcast/episode1/

有没有办法将所有尾部斜杠版本重定向到非尾部斜杠版本?

这里的问题更严重,因为这些都不起作用:

example.com/podcast
example.com/podcast/

只有这个有效:

example.com/podcast.html

我不希望 html 扩展可见。

这是我目前在 .htaccess 文件中的代码:

#shtml

AddType text/html .html
AddHandler server-parsed .html

#html

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

#index redirect

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/ 
RewriteRule ^index\.html$ http://example.com/ [R=301,L]

#non www to www

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

你能帮帮我吗?

【问题讨论】:

标签: html .htaccess redirect slash trailing


【解决方案1】:

删除尾部斜杠很容易。删除 .html 不是。

删除斜线

如果您在此处看到斜线,只需 R=301 重定向。

RewriteRule ^(.*)/$ $1 [R=301]

不要添加L 标志,因为您想继续处理此请求。也让它成为第一条规则。

为什么你不能删除.html

您的问题是,一旦page 变为page.html(通过内部重定向),就会向服务器发出对page.html 请求。那么您的 .htaccess 将看到对page.html 的请求并重定向到page。提示无限循环。

优化您的代码

  1. 您只需要在.htaccess 顶部出现一次RewriteEngine On
  2. 您应该将www 重定向添加到代码顶部并删除L 标志(请参阅删除斜线

【讨论】:

  • 嗨,斯科特,感谢您的帮助。该代码部分有效,部分无效。访问the trailing slash version 看看会发生什么(non trailing slash version 仍然有效)。知道什么可能导致它走很长的路吗?
  • 如何去掉 URL 的 home/theev8/public_html/ 部分?
  • 这是您的RewriteBase 的问题。您可以将其设置为/,这应该可以解决问题 - 在RewriteEngine On 下,添加RewriteBase /。这会改变重写路径 - Apache Mod_Rewrite Documentation
  • 现在它告诉我我有太多重定向。唔。 http://www.pavlinaplus.com/podcast/
  • 我认为问题在于它们是目录,我的猜测是删除 / 会导致它重新添加它(因为它是一个目录)并且它会立即再次删除它,并且很快。目前我真的没有太多时间给自己,但我无法在我的本地机器上重现这个(然而值得注意的是,它在重定向到 .html 之前一直把斜线放回去页)。您最好的选择可能是将index.html 放入文件夹并以这种方式访问​​/foldername.html,或者在斜线删除规则中添加RewriteCond %{REQUEST_FILENAME} !-d
猜你喜欢
  • 2019-03-28
  • 2014-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多