V
V
Valery Selitsky2013-07-17 12:37:10
Apache HTTP Server
Valery Selitsky, 2013-07-17 12:37:10

.htaccess, mod_rewrite and duplicate slashes?

Good afternoon, to remove duplicate slashes, I use a regular expression of the form:

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

But a problem arose, and it lies in the fact that this regular expression is not global, each block of slashes is processed by a separate pass, respectively, by a separate request, and the following parsley is obtained in the log:
127.0.0.1 - - [17/Jul/2013:12:23:04 +0300] "GET ////WAT////no////u//must/be////kidding/////me// HTTP/1.1" 301 269
127.0.0.1 - - [17/Jul/2013:12:23:04 +0300] "GET /WAT/no////u//must/be////kidding/////me// HTTP/1.1" 301 266
127.0.0.1 - - [17/Jul/2013:12:23:04 +0300] "GET /WAT/no/u//must/be////kidding/////me// HTTP/1.1" 301 265
127.0.0.1 - - [17/Jul/2013:12:23:04 +0300] "GET /WAT/no/u/must/be////kidding/////me// HTTP/1.1" 301 262
127.0.0.1 - - [17/Jul/2013:12:23:04 +0300] "GET /WAT/no/u/must/be/kidding/////me// HTTP/1.1" 301 258
127.0.0.1 - - [17/Jul/2013:12:23:04 +0300] "GET /WAT/no/u/must/be/kidding/me// HTTP/1.1" 301 257
127.0.0.1 - - [17/Jul/2013:12:23:04 +0300] "GET /WAT/no/u/must/be/kidding/me/ HTTP/1.1" 404 226

Can you please tell me if it is possible to replace all duplicate slashes within a single pass using .htaccess, and how?
PS Additional, bonus question: I couldn't use Apache to replace the duplicated slashes from the root without query. For example http://test.ru///////, it is not caught by a rewrite, no matter how hard I try. In the upper case, these slashes are removed during the internal Apache redirect, but not by rewriting, this can be seen from the second line of the log, in which it is clear that the slashes disappeared not only at the beginning, but also before “no”, which was done by mod_rewrite.

Answer the question

In order to leave comments, you need to log in

6 answer(s)
K
khipster, 2016-03-16
@WaveCut

2 years have passed. Catch the solution:

# Редирект на страницу без многократных /.
RewriteCond %{THE_REQUEST} //
RewriteRule .* /$0 [R=301,L]

P
PaulZi, 2013-07-17
@PaulZi

Option without crutches:

RewriteCond %{REQUEST_URI} ^(.*?)/{2,}(.*?/{2,}.*?)$ [NC]
RewriteRule . %1/%2 [L]

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

M
mayorovp, 2013-07-17
@mayorovp

I'm no expert, but try something like this:

RewriteRule ^Simplify/(.*?)/{2,}(.*?) Simplify/%1/%2 [N,C]
RewriteRule ^Simplify/(.*?) %1 [R=301,L]
RewriteRule ^(.*?)/{2,}(.*?) Simplify/%1/%2 [N]

P
PaulZi, 2013-07-17
@PaulZi

As a crutch, I can suggest redirecting requests in which there are two slashes in a row to a script (without the "R" flag), which will already clean up the URL and issue the correct redirect.

X
xmoonlight, 2013-07-18
@xmoonlight

See in my signature (about)

I
ipboyar, 2015-11-20
@ipboyar

For posterity:
A slightly unexpected solution
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$ [NC]
RewriteRule ^(.*)$ $1 [L,R=301]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question