P
P
PO6OT2015-07-03 13:33:53
Apache HTTP Server
PO6OT, 2015-07-03 13:33:53

What to write in htaccess so that when a folder is requested, a slash is not added to the path?

These rules cause a circular redirect if a folder is requested because they remove the slash from the path and something unknown adds it

RewriteBase /
RewriteCond %{HTTP_HOST} (.*)
RewriteCond %{REQUEST_URI} /$ [NC]
RewriteRule ^(.*)(/)$ $1 [L,R=301]

Answer the question

In order to leave comments, you need to log in

3 answer(s)
C
Cat Anton, 2015-07-03
@woonem

httpd.apache.org/docs/2.2/mod/mod_dir.html#directo...
Add .htaccess to the top:
DirectorySlash Off

A
Artyom Starchenko, 2015-07-03
@Starche

Something unknown cannot be added. Are you by any chance using some framework based on the symfony router? Symfony, Silex, Lavarel, Lumen? I also suffered with him with the same problem, it is necessary to carefully register routing with them so that this does not happen. I won't be more specific, it depends on how you implement it.

S
ShamblerR, 2015-07-03
@ShamblerR

############################################################################
####  убирает /  в конце строки если это файл ( содержит точку в урл)   ####
############################################################################
RewriteCond %{REQUEST_URI} \..+$
            #содержит точку (файл)
RewriteCond %{REQUEST_FILENAME} !-d
            #не директория
RewriteCond %{REQUEST_FILENAME} -f
             #является файлом
RewriteCond %{REQUEST_URI} ^(.+)/$
             #в конце урла есть слеш
RewriteRule ^(.+)/$ /$1 [R=301,L]
             #исключить слеш

############################################################################
####  Добавляет  /   если его нет, и это не файл!!!                     ####
############################################################################
RewriteCond %{REQUEST_URI} !(.*)/$
            #слеша в конце нет
RewriteCond %{REQUEST_FILENAME} !-f
            #является файлом
RewriteCond %{REQUEST_URI} !\..+$
            #в урле нет точки (файл)
RewriteRule ^(.*)$ $1/ [L,R=301,QSA]
            #добавляем слеш в конце

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question