通过 .htaccess 用 .html 替换 .php ext

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4548860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:25:26  来源:igfitidea点击:

replacing .php ext with .html through .htaccess

php.htaccess

提问by vector

Greetings!

你好!

I'm trying to replace .php extensions with .html

我正在尝试用 .html 替换 .php 扩展名

So far I got:

到目前为止,我得到了:

RewriteRule ^(.*)\.html .php

... it works nicely when url like /site/page.htmlis entered (and page.htmldoes not physically exist but page.phpdoes).

...当/site/page.html输入url like 时它工作得很好(并且page.html实际上不存在但page.php确实存在)。

However what I'd like to have is when /site/page.phpis entered the viewer sees only /site/page.htmlin the browser location.

但是,我想要的是当/site/page.php进入时查看者只能/site/page.html在浏览器位置看到。

Is that doable or do I have to set up explicit redirects for each page? :-(

这是可行的还是我必须为每个页面设置显式重定向?:-(

Thanks in advance.

提前致谢。

ps: dev environment I'm using is XAMPP on os x if it makes any difference

ps:我使用的开发环境是 os x 上的 XAMPP,如果它有什么不同的话

回答by

RewriteRule ^(.*)\.php .html [R=301,L]
RewriteRule ^(.*)\.html .php

edit after pulling white rabbit out of the hat

把白兔从帽子里拉出来后编辑

RewriteEngine on  
RewriteBase /

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

RewriteCond %{THE_REQUEST} (.*)\.html  
RewriteRule ^(.*)\.html .php [L]  

回答by manglesh upadhyay

RewriteEngine on  
RewriteBase /

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

RewriteCond %{THE_REQUEST} (.*)\.html  
RewriteRule ^(.*)\.html .php [L]

回答by THE DOCTOR

Give this a try:

试试这个:

RewriteEngine on
RewriteBase /
RewriteRule ^(.*)\.html$ .php [L] 

回答by Jan ?afránek

I think that this is much simple

我认为这很简单

AddType application/x-httpd-php .html

it just process every .html file as normal .php

它只是像正常的 .php 一样处理每个 .html 文件

if you want it only for current dir

如果你只想要当前目录

<Files *.html>
ForceType application/x-httpd-php
</Files>