hide extension .php in url mod_rewrite

后端 未结 3 1681
渐次进展
渐次进展 2020-12-09 14:37

I have a website in PHP, and I\'m trying to hide the extension. I\'ve found a couple of things via Google, but they all seem to be too complicated, or they redirect in

相关标签:
3条回答
  • 2020-12-09 14:43

    The htacces file should look like this:

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php
    

    You redirect any php file in the url with the same name withouth the "php".
    Then in your php files, you can check to see if the url contains the extension (http://blabla.com/bla.php) and redirect the page to the same one withouth the extension.
    So, at the beginning of each php file you should call this function :

    function redirectIfNeeded(){
        $url = $_SERVER["REQUEST_URI"];
        if(preg_match("/\.php/$", $url))
            header("Location: ".preg_replace("/\.php/",$url));
    }
    
    0 讨论(0)
  • 2020-12-09 15:00

    Please crate a .htaccess file in root directory an use this code!!

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

    Hope!! it would be useful for someone!

    0 讨论(0)
  • 2020-12-09 15:06
    RewriteEngine on
    
    Rewritecond %{REQUEST_URI} !-f Rewritecond %{REQUEST_URI} !-d Rewritecond %{REQUEST_URI} !-l RewriteRule ^([\w\d-]+)$ $1.php [L]
    
    RewriteRule ^([^/.]+)$ $1.php [L]
    
    0 讨论(0)
提交回复
热议问题