htaccess remove .php extension, index.php AND add trailing slash

前端 未结 2 1662
别跟我提以往
别跟我提以往 2021-01-01 08:07

This is for basic HTML/PHP pages, no query strings, etc.. I have searched high and low and find resources for removing the \'index.php\' from the URI, or removing \'.php\' a

2条回答
  •  隐瞒了意图╮
    2021-01-01 08:32

    I got this from a rather helpful chap on a forum once - never fully understood it, and there is one caveat; it implies no trailing slash unless the request is a directory.

    However, I thought it was worth posting - a guru out there may easily spot the fix!?

    # remove .php; use THE_REQUEST to prevent infinite loops
    RewriteCond %{HTTP_HOST} ^www\.domain\.com
    RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
    RewriteRule (.*)\.php$ $1 [R=301]
    
    # remove index
    RewriteRule (.*)index$ $1 [R=301]
    
    # remove slash if not directory
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} /$
    RewriteRule (.*)/ $1 [R=301]
    
    # add .php to access file, but don't redirect
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteCond %{REQUEST_URI} !/$
    RewriteRule (.*) $1\.php [L]
    

提交回复
热议问题