dongtao4787 2014-11-10 10:26
浏览 40
已采纳

用.html替换.html通过.htaccess替换根文件而不是子目录文件

How we can replace .php ext with .html through .htaccess for root files only not subdirectoy files.

Example : www.test.com/home.php should be change to www.test.com/home.html but www.test.com/admin/home.php should remain same.

I simply tried as below:

RewriteEngine on  
RewriteBase /

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

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

Please assist me.

  • 写回答

2条回答 默认 最新

  • duancai7568 2014-11-10 10:39
    关注

    You can use code like this in root .htaccess:

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

    [^/.]+ regex will make sure to make it work in root path only.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复