2

I am unable to write a rule that matches double slashes.

In my .htacess file:

#RULE 1:  
RewriteCond %{REQUEST_URI} ^.*hi1.*$ 
RewriteRule ^.*$ https://www.google.com/ [R=301,L] 

#RULE 2: 
RewriteCond %{REQUEST_URI} ^.*hi2/.*$ 
RewriteRule ^.*$ https://www.google.com/ [R=301,L] 

#RULE 3: 
RewriteCond %{REQUEST_URI} ^.*hi3//.*$ 
RewriteRule ^.*$ https://www.google.com/ [R=301,L] 

RESULTS:

https://www.example.com/hi1//  
successfully redirects to google  

https://www.example.com/hi2//
successfully redirects to google  

https://www.example.com/hi3//
fails to redirect to google

Third url yields the following:
Sorry, this page doesn't exist.
Please check the URL or go back a page.
404 Error. Page Not Found.

EDIT # 1:

Interestingly:

#RULE 4: 
RewriteCond %{REQUEST_URI} ^.*hi4/.*/.*$ 
RewriteRule ^.*$ https://www.google.com/ [R=301,L] 

RESULTS:

https://www.example.com/hi4/abc/  
successfully redirects to google 

https://www.example.com/hi4//
fails to redirect to google

EDIT # 2:

My original post seems to have created confusion. I will try to be clearer: I need a rule that will match a url ending in double slash, and will not match a url that does not end in double slash. Currently, my .htaccess file contains only the following:

RewriteEngine on

RewriteRule yoyo https://www.cnn.com/ [R=301,L]  

RewriteCond %{THE_REQUEST} //$ 
RewriteRule ^.*$ https://www.google.com/ [R=301,L]  

Results:

https://www.example.com/about-us//
fails to redirect to google, and yields 404 error

(The first rule (yoyo) is only to ensure no caching.)

EDIT # 3:

I see that the confusion continues. So, my .htaccess file contains only:

RewriteEngine on
RewriteCond %{THE_REQUEST} //$ 
RewriteRule ^.*$ https://www.google.com/ [R=301,L]  

Results:

https://www.example.com/about-us//
fails to redirect to google, and yields 404 error

This time, I think we can rule out caching, because I used the .htaccss on a website of mine that previously had no .htaccess file.

Simply, my efforts to match a url ending with double-slash are failing.

oyvey
  • 609
  • 1
  • 9
  • 20
  • 1
    Does this answer your question? [Issue In Removing Double Or More Slashes From URL By .htaccess](https://stackoverflow.com/questions/17080652/issue-in-removing-double-or-more-slashes-from-url-by-htaccess) – oguzhancerit Dec 02 '20 at 13:32
  • 2
    This condition should work `RewriteCond %{THE_REQUEST} ^.*hi3//.*$ ` – Amit Verma Dec 02 '20 at 13:46
  • oguzhancerit: Thanks so much. Those didn't work for me. Amit: Thanks so much. It sounds like you're telling me that my third rule actually should work. I cannot understand why it is not. – oyvey Dec 02 '20 at 13:55
  • 1
    @ Oyvey Try clearing your browser cache and change `REQUEST_URI` to `THE_REQUEST` . – Amit Verma Dec 02 '20 at 14:06
  • 1
    As mentioned above, rule #3 and #4 should work as intended and trigger a redirect for the cases stated. Do you have any other directives in your `.htaccess` file? Caching has been mentioned above, but you should be testing this with a 302 (temporary) redirect to avoid caching issues. 301s are cached persistently by the browser by default, so can make testing problematic. – MrWhite Dec 02 '20 at 16:11
  • Thanks to all who replied! 1. anubhava: thx so much for re-open! 2. Amit: I tried THE_REQUEST -- same problem. 3. MrWhite: In my actual .htaccess file, I am constantly revising the patterns to ensure no caching. 4. oguzhancerit: I tried the solutions of that url before posting here. Didn't work for me. – oyvey Dec 03 '20 at 14:47
  • "In my actual .htaccess file, I am constantly revising the patterns to ensure no caching" - Although that is prone to error and time consuming (having to update your tests all the time). You should test with 302s and with the _browser inspector_ and "Disable cache" checked on the "Network" tab. A conflict with other directives could prevent `REQUEST_URI` from working, which is why I asked about "other directives" in a comment above. However, `THE_REQUEST` should work (depending on the pattern) - you should debug this by assigning this value to an env var and echo'ing this to your page. – MrWhite Dec 04 '20 at 11:06
  • "(The first rule (yoyo) is only to ensure no caching.)" - That doesn't make sense. That doesn't stop the response from the second rule being cached. (?) – MrWhite Dec 04 '20 at 18:59

4 Answers4

2

You need not to write 3 rules when you could catch similar kind of URIs with regex patterns so that we need not to write multiple patterns, this also takes cares of multiple occurrences of / coming in the end. Could you please try following, please make sure you clear your browser cache after placing these rules into your htaccess file.

RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/hi[0-9]+/{2,}?$ [NC] 
RewriteRule ^(.*)$ https://www.google.com/ [R=301,L] 
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • 1
    1. Thank you so much for your reply. I'm aware that I don't need 3 rules. They are to pinpoint what works and what doesn't, and that I'm able to match one slash, but not two. 2. Your solution redirects example.com/hi3// but also example.com/hi3/ - I need a solution that matches url's with double slashes and only url's with double slashes. – oyvey Dec 03 '20 at 14:49
  • @oyvey, for double or more slashes I checked it worked fine for me and for redirect I thought you want to redirect all h then digits urls? Isn't it the case here, please confirm once. – RavinderSingh13 Dec 03 '20 at 14:50
  • 1
    "for double or more slashes" - But the pattern `/+` matches 1 or more slashes. – MrWhite Dec 04 '20 at 10:57
  • @MrWhite, Thank you sir, missed that part, fixed the regex here, I hope this should be good now. – RavinderSingh13 Dec 04 '20 at 10:59
  • @oyvey, pattern in my rules `{2,}?$` is matching 2 or more occurrences of `/`. In case of matching only 2 `/` try changing `{2,}?$` --> `//?$` and let me know if that helps you? – RavinderSingh13 Dec 04 '20 at 17:35
  • Ravinder: I need a pattern that will match 2 consecutive slashes, and that will not match 1 slash. – oyvey Dec 04 '20 at 17:39
  • @oyvey, ok so you mean either 1 OR 2 consecutive occurrences of `/`. So try this one `/{1,2}?$` once in my rule `RewriteCond %{REQUEST_URI} ^/hi[0-9]+/{1,2}?$ [NC]` and let me know how it goes please. – RavinderSingh13 Dec 04 '20 at 17:41
  • Hi, Ravinder: I need a rule that will match only url's containing 2 (or more) consecutive slashes. Please see edit # 2 of my OP. Thanks so much for your efforts! – oyvey Dec 04 '20 at 18:00
  • @oyvey, then `RewriteCond %{REQUEST_URI} ^/hi[0-9]+/{2,}$ [NC]` may help you, could you please check it once and let me know if this helps you? – RavinderSingh13 Dec 04 '20 at 18:02
2

EDIT: OK now I get it. Only match paths ending with two slashes.

I updated the answer. The request URI inside THE_REQUEST is not on the end, but is followed by a space and more after that, so matching //\s should work for you

AmitVerma mentioned the correct answer in his comment, but it is being snowed in by other comments. For all the other people like me who did not know about the THE_REQUEST parameter (thank you Amit) a more complete answer here.

The problem with the original rule is the use of the REQUEST_URI parameter. The value of this parameter will probably already have been cleaned by the webserver or other modules. Double slashes would have been removed.

The THE_REQUEST parameter contains the original unmodified request. Therefore the following will work as requested:

RewriteCond %{THE_REQUEST} //\s.*$
RewriteRule ^.*$ https://www.google.com/ [R=301,L] 
Thakkie
  • 604
  • 4
  • 17
  • Although (confusingly) the OP states they have already tried using `THE_REQUEST` with the "same problem". However, in the limited scope of the question, `REQUEST_URI` should also work. This will not have been "cleaned" (apart from being %-decoded) by "the webserver or other modules" (unlike the URL matched by the `RewriteRule` _pattern_), except that other directives in the config/`.htaccess` file might have internally rewritten the URL, which could have changed the `REQUEST_URI` server variable. But, as you suggested, using `THE_REQUEST` should have gotten around this problem. – MrWhite Dec 04 '20 at 10:56
  • Thank you for your post, Thakkie. I tried Amit's suggestion, and it didn't help. It produced the same result as my rule # 3: no redirect, and instead, the 404 error in my OP. – oyvey Dec 04 '20 at 17:45
  • Too bad. I tested this rule/condition combo with both REQUEST_URI and THE_REQUEST on apache 2.4.18 and I actually noticed a difference where one worked as promised and the other did not. And just to be sure tested it again just now. I hope you find out what is going wrong in your specific situation. – Thakkie Dec 04 '20 at 18:31
2

Regarding your updated question:

... I need a rule that will match a url ending in double slash

RewriteCond %{THE_REQUEST} //$ 
RewriteRule ^.*$ https://www.google.com/ [R=301,L]

Aside: Your previous rules matched a URL containing a double slash anywhere in the URL-path (which would naturally catch a double slash at the end as well).

However, the above will not match a URL that ends with a double slash. In fact, it will never match anything because THE_REQUEST does not only contain the URL. THE_REQUEST server variable contains the first line of the HTTP request headers. For example, when you request https://example.com/about-us//, THE_REQUEST will contain a string of the form:

GET /about-us// HTTP/1.1

So, you can see from the above that a regex like //$ will never match. You will need to use a condition of the form:

RewriteCond %{THE_REQUEST} //\s

To match two slashes followed by a space. Which could only occur at the end of URL. (Although it could also occur at the end of the query string, but cross that bridge when we come to it.)

However, since the other suggestions (eg. ^.*hi3//.*$) don't appear to have worked, then this is not going to work either.

You need to clear your browser cache before testing and please test with 302 (temporary) redirects, otherwise, you can easily go round in circles chasing caching issues. You should also test this with the Browser "Inspector" open on the "Network" tab and check the "Disable cache" option. For example, in Chrome:

Disable Cache in Chrome Inspector

(UPDATE) Debugging...

This does not seem to be a question about regex, as the earlier answers/comments (and code snippets in the question itself) should already have produced the desired results. So "something else" would seem to be going on here.

To debug and see the value of THE_REQUEST, you can do something like the following (at the very top of your .htaccess file):

RewriteCond %{QUERY_STRING} !^the-request=
RewriteRule ^ /?the-request=%{THE_REQUEST} [R,L]

And then request /about-us//. You should then be redirected to a URL of the form:

/?the-request=GET%20/about-us//%20HTTP/1.1

(Where the %20 are naturally the URL encoded spaces.)

Please report back exactly what you are seeing.

MrWhite
  • 43,179
  • 8
  • 60
  • 84
  • Thank you for sharing this, I was trying `RewriteCond %{THE_REQUEST} \shi[0-9]+/{2,}+\s [NC] ` but it was picking single `/` also(though OP said 2 or more `/` only), thanks for sharing. – RavinderSingh13 Dec 04 '20 at 18:42
  • 1
    @RavinderSingh13 Thanks. There does seem to be confusion over the exact requirements here, but this confusion is only compounded since the OP appears to be seeing a different response than what the directives "should" be doing - which is a bit of a mystery as yet. (99% of the time this would either be a conflict with existing directives or a caching issue. But in the latest question update they've stated that the "file contains **only the following**" and they are adamant that the browser cache is clear. (?)) – MrWhite Dec 04 '20 at 19:26
1

Here's what finally worked to match double slashes (nothing else worked for me):

RewriteEngine on
RewriteCond %{THE_REQUEST} //
RewriteRule ^.*$ https://www.google.com/ [R=301,L]

(And, as I wrote, I was careful to prevent caching, so caching never was an issue.)

PLOT TWIST:

Even this solution, which is the only solution that works on one of my websites, does not work on the website I have been testing on for most of this discussion. In other words, there is not one single solution for matching double-slash on that server!

oyvey
  • 609
  • 1
  • 9
  • 20
  • 1
    Although this matches a double slash _anywhere_ in the URL, not just at the end (as in your updated question). However, this is pretty much the same as examples already presented. @AmitVerma's initial comment suggested `^.*hi3//.*$` (the same as simply `hi3//`) matches "hi3//" anywhere in the URL, just as `//` matches "//" anywhere in the URL. The recent update to your question (EDIT#3) contains the same _incorrect_ regex as EDIT#2, yet two of the existing answers (including mine) had already corrected this (to use `//\s` instead of `//$` - to match a double slash at the _end_ of the URL). – MrWhite Dec 06 '20 at 12:04
  • This is also _identical_ to [an answer on the linked question](https://stackoverflow.com/a/50256295/369434), which you stated "didn't work for me."? – MrWhite Dec 06 '20 at 12:06