Htaccess: add/remove trailing slash from URL

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

    0 讨论(0)
  • 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)

    0 讨论(0)
  • 2020-11-22 06:05
    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /
    ## hide .html extension
    # To externally redirect /dir/foo.html to /dir/foo
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+).html
    RewriteRule ^ %1 [R=301,L]
    
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)/\s
    RewriteRule ^ %1 [R=301,L]
    
    ## To internally redirect /dir/foo to /dir/foo.html
    RewriteCond %{REQUEST_FILENAME}.html -f
    RewriteRule ^([^\.]+)$ $1.html [L]
    
    
    <Files ~"^.*\.([Hh][Tt][Aa])">
    order allow,deny
    deny from all
    satisfy all
    </Files>
    

    This removes html code or php if you supplement it. Allows you to add trailing slash and it come up as well as the url without the trailing slash all bypassing the 404 code. Plus a little added security.

    0 讨论(0)
  • 2020-11-22 06:08

    This is what I've used for my latest app.

    # redirect the main page to landing
    ##RedirectMatch 302 ^/$ /landing
    
    # remove php ext from url
    # https://stackoverflow.com/questions/4026021/remove-php-extension-with-htaccess
    RewriteEngine on 
    
    # File exists but has a trailing slash
    # https://stackoverflow.com/questions/21417263/htaccess-add-remove-trailing-slash-from-url
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^/?(.*)/+$ /$1 [R=302,L,QSA]
    
    # ok. It will still find the file but relative assets won't load
    # e.g. page: /landing/  -> assets/js/main.js/main
    # that's we have the rules above.
    RewriteCond %{REQUEST_FILENAME} !\.php
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f 
    RewriteRule ^/?(.*?)/?$ $1.php
    
    0 讨论(0)
提交回复
热议问题