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条)

报告相同问题?

悬赏问题

  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile
  • ¥15 写uniapp时遇到的问题
  • ¥15 vs 2008 安装遇到问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题