Htaccess: add/remove trailing slash from URL

前端 未结 4 1619
时光取名叫无心
时光取名叫无心 2020-11-22 05:36

My website runs a script called -> WSS wallpaper script

My Problem -> I have been trying to force remove or add trailing slash to the end of my URL

4条回答
  •  执笔经年
    2020-11-22 05:57

    To complement Jon Lin's answer, here is a no-trailing-slash technique that also works if the website is located in a directory (like example.org/blog/):

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [R=301,L]
    


    For the sake of completeness, here is an alternative emphasizing that REQUEST_URI starts with a slash (at least in .htaccess files):

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} /(.*)/$
    RewriteRule ^ /%1 [R=301,L] <-- added slash here too, don't forget it
    

    Just don't use %{REQUEST_URI} (.*)/$. Because in the root directory REQUEST_URI equals /, the leading slash, and it would be misinterpreted as a trailing slash.


    If you are interested in more reading:

    • PR 3145 for Laravel
    • A discussion on commit 343c31e

    (update: this technique is now implemented in Laravel 5.5)

提交回复
热议问题