RewriteRule htaccess to always remove trailing slash even directory

前端 未结 2 1789
臣服心动
臣服心动 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]
    
    0 讨论(0)
  • 2021-01-16 03:35

    There is a separate setting in apache called DirectorySlash that can be enabled/disabled. You can read more at httpd.apache.org/docs/2.2/mod/mod_dir.html#directoryslash but be sure to read the part about why this is done right below where it says "some good reasons". Also note the security issue.

    0 讨论(0)
提交回复
热议问题