L
L
lexstile2019-09-23 11:55:59
htaccess
lexstile, 2019-09-23 11:55:59

How to redirect from slash to without slash the entire site?

I try like this:

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

It only comes out for pages that exist in the database, that is, I have a single entry point and all pages are drawn from the database (it works with them).
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php

But, now I have created a physical folder, placed index.php in it - a cyclic redirect comes out:

Site.ru redirected too many times.
Delete cookies..
ERR_TOO_MANY_REDIRECTS

I tried to redirect the other way around - to a slash (everything works):
RewriteCond %{REQUEST_URI} !\?
RewriteCond %{REQUEST_URI} !\&
RewriteCond %{REQUEST_URI} !\=
RewriteCond %{REQUEST_URI} !\.
RewriteCond %{REQUEST_URI} !\/$
RewriteRule ^(.*[^\/])$ /$1/ [R=301,L]

What could be the reason?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dodo512, 2019-09-23
@lexstile

created a physical folder, placed index.php in it - a cyclic redirect comes out

Usually DirectorySlash is enabled by default and for existing folders mod_dir makes a redirect with a slash added.
You can avoid looping like this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]

Or add , but then additional rules are needed so that when accessing an existing folder, the index.php placed in it works. Or don't bother and leave the addresses with a slash. DirectorySlash Off

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question