【问题标题】:replacing .php ext with .html through .htaccess通过 .htaccess 将 .php ext 替换为 .html
【发布时间】:2011-05-31 17:39:54
【问题描述】:

您好!

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

到目前为止,我得到了:

RewriteRule ^(.*)\.html $1.php

...当输入像 /site/page.html 这样的 url 时效果很好(page.html 实际不存在但 page.php 存在)。

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

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

提前致谢。

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

【问题讨论】:

  • 如果你走那么远,为什么还要有一个扩展?只需让 /site/page 引用 .php 页面。它看起来更干净。
  • 您可以查看本教程,了解如何使用 htaccess helponnet.com/2021/04/27/….php 替换为 .html

标签: php .htaccess


【解决方案1】:
RewriteEngine on  
RewriteBase /

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

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

【讨论】:

  • 应用此解决方案后,.php 页面作为 .html 可以正常工作,但现有 .html 页面已停止工作。您能否帮忙解决现有 .html 文件的问题?
【解决方案2】:

我认为这很简单

AddType application/x-httpd-php .html

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

如果你只想要当前目录

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

【讨论】:

    【解决方案3】:
    RewriteRule ^(.*)\.php $1.html [R=301,L]
    RewriteRule ^(.*)\.html $1.php
    

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

    RewriteEngine on  
    RewriteBase /
    
    RewriteCond %{THE_REQUEST} (.*)\.php  
    RewriteRule ^(.*)\.php $1.html [R=301,L]  
    
    RewriteCond %{THE_REQUEST} (.*)\.html  
    RewriteRule ^(.*)\.html $1.php [L]  
    

    【讨论】:

    • ...出了点问题,当我输入“localhost/site/index.php”时,我得到“localhost/Applications/XAMPP/xamppfiles/htdocs/site/index.html”,不知道该怎么办
    • 这让我的 Apache 陷入无限循环
    • ...重写基础没有帮助:-(
    • ...做到了!除了处理 XAMPP 的特殊性之外,对于 rewrie 库,我必须使用 '/site/' 而不仅仅是 '/' 这就是我不定期使用这些东西时会发生的情况 :-) 谢谢
    • 应用此解决方案后,.php 页面作为 .html 可以正常工作,但现有 .html 页面已停止工作。您能否帮忙解决现有 .html 文件的问题?
    【解决方案4】:

    试试这个:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-02
      • 1970-01-01
      • 2021-07-22
      • 1970-01-01
      • 2013-01-30
      • 1970-01-01
      • 2013-09-12
      相关资源
      最近更新 更多