301通过.htaccess将* .php重定向到* .html吗?

编程入门 行业动态 更新时间:2024-10-26 20:30:22
本文介绍了301通过.htaccess将* .php重定向到* .html吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

Currently I'm rewriting all incoming requests for *.html to *.php in my .htaccess:

RewriteEngine on RewriteCond %{HTTP_HOST} ^example\$ [NC] RewriteRule ^(.*)$ www.example/$1 [R=301,L] RewriteRule ^(.*).html$ $1.php [QSA] ErrorDocument 404 /404.html

因此/something.html被重写为/something.php。

So /something.html is rewritten to /something.php.

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

However, /something.php is still directly accessible in the browser. Now I want it to redirect to /something.html when people are accessing it in the browser, so as to avoid 2 distinct URLs for the same page of content.

是这可能在我的.htaccess文件中执行?怎么样?我尝试了R = 301,但它始终是重定向循环之类的东西。任何帮助,将不胜感激。谢谢!

Is this possible to do in my .htaccess? How? I tried R=301 but it's always a redirect loop or something. Any help would be appreciated. Thanks!

推荐答案

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

Add this above your RewriteRule ^(.*).html$ $1.php [QSA] rule:

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

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

That will redirect the browser/client to the same request but with a .html instead of a .php.

更多推荐

301通过.htaccess将* .php重定向到* .html吗?

本文发布于:2023-10-31 19:19:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1547033.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重定向   htaccess   html   php

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!