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

报告相同问题?