RewriteRule htaccess to always remove trailing slash even directory

前端 未结 2 1814
臣服心动
臣服心动 2021-01-16 02:58

The target is to combine several rules:

  • never have a trailing slash in the URI
  • internally rewrite to the index.php (domain.tld/somedir/index.php) when
2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-16 03:34

    You can use this code:

    DirectoryIndex index.php
    RewriteEngine On
    
    # remove trailing slash
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
    RewriteRule ^(.+?)/$ /$1 [R=301,L]
    
    # To internally forward /dir/file to /dir/file.php
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{DOCUMENT_ROOT}/$1.php -f [NC]
    RewriteRule ^(.+?)/?$ /$1.php [L]
    

提交回复
热议问题