【问题标题】:301 Redirect *.php to *.html via .htaccess?301 通过 .htaccess 将 *.php 重定向到 *.html?
【发布时间】:2013-09-12 15:06:52
【问题描述】:

目前,我正在将所有传入的 *.html 请求重写为我的 .htaccess 中的 *.php:

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

RewriteRule ^(.*).html$ $1.php [QSA]

ErrorDocument 404 /404.html

所以 /something.html 被重写为 /something.php。

但是,/something.php 仍然可以在浏览器中直接访问。现在,当人们在浏览器中访问它时,我希望它重定向到 /something.html,以避免同一内容页面出现 2 个不同的 URL。

这可以在我的 .htaccess 中进行吗?如何?我试过 R=301 但它总是一个重定向循环或其他东西。任何帮助,将不胜感激。谢谢!

【问题讨论】:

  • 能否说明=> [R=301,L] 的含义?

标签: .htaccess redirect seo


【解决方案1】:

将此添加到您的 RewriteRule ^(.*).html$ $1.php [QSA] 规则上方:

RewriteCond %{THE_REQUEST} \ /(.+)\.php
RewriteRule ^ /%1.html [L,R=301]

这会将浏览器/客户端重定向到相同的请求,但使用 .html 而不是 .php

【讨论】:

  • 能否说明=> [L,R=301] 的含义?
  • @PratikJoshi 他们是Rewrite flags,“L”表示“Last”,停止当前迭代的重写,“R”表示“Redirect”,301 是重定向代码(永久)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-21
  • 2017-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多