I'm in situation when CMS creates duplicates. Every page can be opened with slash at the end and without.
So I need to rewrite physically every URL for Search Engine and people with 301 redirect to condition when it's without slash at the end.
But the problem is the script then stops working because it works only with slashes at the URLs' end.
I've made logically this .htaccess
RewriteCond %{REQUEST_URI} ^(.*)/$ [NC]
RewriteRule ^(.*)(/) $1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*[^/])$ $1/ [L]
But it's getting redirect loop. Please could you help me to write it correct. The main purpose that users and SE should see no end-slash URL but script needs on the server internally rewritten URL with end-slash.
But it's getting redirect loop. Please could you help me to write it correct. The main purpose that users and SE should see no end-slash URL but script needs on the server internally rewritten URL with end-slash.
You need to match against the actual request if you're going to redirect, because otherwise, the URI gets rewritten, and your first rule matches against it and redirects (again, and again, and again).
So something like this:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]*?)/($|\ )
RewriteRule ^(.*)(/) $1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*[^/])$ $1/ [L]
(only the first condition was changed)