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 前后端分离的学习疑问?
  • ¥15 stata实证代码答疑
  • ¥15 MATLAB数据处理插值
  • ¥50 husky+jaco2实现在gazebo与rviz中联合仿真
  • ¥15 dpabi预处理报错:Error using y_ExtractROISignal (line 251)
  • ¥15 在虚拟机中配置flume,无法将slave1节点的文件采集到master节点中
  • ¥15 husky+kinova jaco2 仿真
  • ¥15 zigbee终端设备入网失败
  • ¥15 金融监管系统怎么对7+4机构进行监管的
  • ¥15 硬件IIC从模式的数据发送,中断数据的接收,不能用HAL库(按照时序图)