htaccessinifinte循环当试图添加/? - htaccess inifinte loop when trying to add “/”

- 此内容更新于:2015-12-20



I want to add "/" at the end of the url, it is being done automatically for directories but i also want to do it for non existing directories, which i use for pretty page url's

RewriteCond %{REQUEST_URI} [^\.].*[^/]$
RewriteRule .? %{REQUEST_URI}/ [R=301,L]

What i think it says is if request_uri has no "." in it and doesn't end with a "/" redirect to request_uri/

What happens is i get a redirect from mylink to mylink/ as expected but it keeps going so i end up with mylink////////// (at 10 browser detects infinite loop)

I guess theres a problem with my regex but i just can't figure it out.


You can use the following :

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

This adds a trailing slash to the redirected url. The issue you described in your post is a little diffrent. Infinite loop error is when a url is pointing to itself, your orignal code is correct ,it successfully adds a trailing slash .your other rules may be conflicting with this one.


(原文:This is still too much black magic for me i think browser cache was involved in the issue since i test same url with different rules over and over and now it just works.)