Remove .php extension with .htaccess

前端 未结 15 2071
一生所求
一生所求 2020-11-21 04:32

Yes, I\'ve read the Apache manual and searched here. For some reason I simply cannot get this to work. The closest I\'ve come is having it remove the extension, but it point

15条回答
  •  南笙
    南笙 (楼主)
    2020-11-21 05:12

    In addition to other answers above,

    You may also try this to remove .php extensions completly from your file and to avoid infinite loop :

    RewriteEngine On
    RewriteBase /
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
    RewriteRule ^ %1 [R=301,L]
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.*?)/?$ $1.php [NC,L]
    

    This code will work in Root/.htaccess, Be sure to change the RewriteBase if you want to place this to a htaccess file in sub directory.

    Edit :

    On apache 2.4 and later, you can also use the END flag to prevent infinite loop error. The following example works same as the above on apache 2.4 ,

    RewriteEngine on
    
    RewriteRule ^(.+)\.php$ /$1 [R,L]
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.*?)/?$ /$1.php [NC,END]
    

提交回复
热议问题