mod_rewrite: remove trailing slash (only one!)

前端 未结 5 635
面向向阳花
面向向阳花 2020-11-28 09:38

I use mod_rewrite/.htaccess for pretty URLs.

I\'m using this condition/rule to eliminate trailing slashes (or rather: rewrite to the non-trailing-slash-URL, by a 301

相关标签:
5条回答
  • 2020-11-28 09:46

    If you only want to remove the trailing slashes from GET requests, use the below:

    RewriteCond %{REQUEST_METHOD} =GET
    RewriteRule ^(.*)/$ /$1 [L,R=301]
    
    0 讨论(0)
  • 2020-11-28 09:58

    Here is a mod-alias based solution to remove trailing slash from urls :

    RedirectMatch ^/(.*?)/$ /$1
    

    You can use the above Redirect in your htaccess or server.config file.

    This will redirect /uri/ to */uri** .

    0 讨论(0)
  • 2020-11-28 10:01

    the following rule will match any URL ending in a slash and remove all slashes from the end of it:

    RewriteRule ^(.*)/+$ $1 [R=301,L]
    

    Note: The currently accepted answer only works for http not https but this one works for both.

    0 讨论(0)
  • 2020-11-28 10:05

    change the rewrite rule to:

    RewriteRule ^(.+[^/])/$ http://%{HTTP_HOST}/$1 [R=301,L]
    

    in English: match the start of the string, one or more anything, NOT a slash, a slash, the end.

    0 讨论(0)
  • 2020-11-28 10:10
    ^(.+[^/])/$
    

    I.e. the forelast character must not be a slash.

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