doudou0612 2013-03-19 20:31
浏览 59
已采纳

.htaccess将html重定向到php并访问没有.php扩展名的php文件

I wish to accomplish 2 things with this .htaccess file.

1) All requests to .html point to .php eg. user goes to: http://www.mywebsite.com/contact.html Browser loads: http://www.mywebsite.com/contact.php

2) Request to http://www.mywebsite.com/contact will load the contact.php (This should apply to all pages not just the contact page.

Here is my .htaccess file.

Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L]

To be honest I have no idea what these are doing. I blended them together from a mismash of articles I read. Any help would be appreciated.

  • 写回答

3条回答 默认 最新

  • duanlu9557 2013-03-19 20:37
    关注

    Try something like this. Follow the comment thread for security implications, but this is what you're asking to do

    RewriteEngine On
    RewriteBase /
    
    # Redirect HTML to PHP
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*).html$ $1.php [L]
    
    # Otherwise, try PHP
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !\.php$
    RewriteRule ^(.*)$ $1.php [L]
    
    # Lastly, fallback to error page
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . 404.php [L]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人