Htaccess: add/remove trailing slash from URL

前端 未结 4 1576
时光取名叫无心
时光取名叫无心 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:55

    Right below the RewriteEngine On line, add:

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R] # <- for test, for prod use [L,R=301]
    

    to enforce a no-trailing-slash policy.

    To enforce a trailing-slash policy:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*[^/])$ /$1/ [L,R] # <- for test, for prod use [L,R=301]
    

    EDIT: commented the R=301 parts because, as explained in a comment:

    Be careful with that R=301! Having it there makes many browsers cache the .htaccess-file indefinitely: It somehow becomes irreversible if you can't clear the browser-cache on all machines that opened it. When testing, better go with simple R or R=302

    After you've completed your tests, you can use R=301.

提交回复
热议问题