trzczy
trzczy

Reputation: 1491

.htaccess - Help me understand the behavior

I use xampp on my local PC. The .htaccess file is placed in the /test/ folder. I am testing the address localhost/test on chrome browser.

The .htaccess file is this:

RewriteEngine On
RewriteRule . http://google.com

This redirects to Google. The same as this

RewriteEngine On
RewriteRule .. http://google.com

The same with 3 dots, 4 dots, 5 dots, 6 dots, 7 dots, 8 dots, 9 dots.

But when there are 10 dots or more redirection does not work. So this does not do redirection (10 dots)

RewriteEngine On
RewriteRule .......... http://google.com

What do the dots actually mean in such a .htaccess file? Are they a regex? What is that magic number of 10?

edit I probably found out the reason. The file that is opened on the addresse of localhost/test is index.php. This file name length is actually 9 letters.

So nine or less dots match it but 10 dots do not. I think this is the answear.

edit2 There is still something magic in the dots. Now I have put the .htaccess file to the empty directory of test2 and have started to test the address of localhost/test2.

The redirection takes place up to 13 dots. For 14 dots and more there is no redirection. Confusing.

Upvotes: 0

Views: 105

Answers (3)

trzczy
trzczy

Reputation: 1491

I have just found the answear in apache error log file. This regex pattern of dots is matched against some default file names even if there is only .htaccess file in the directory. In my configuration that default file names are:

index.php
index.pl
index.cgi
index.asp
index.shtml
index.html
index.htm
default.php
default.pl
default.cgi
default.asp
default.shtml

The matching ends at default.shtml file name that has just 13 letters (as 13 dots). Actually that default.shtml file is not present in the directory. Then redirection starts.

Upvotes: 0

Walf
Walf

Reputation: 9308

You're dealing with a standard PCRE. The dots are not magic, each one means 'match any character'. It must be matching 13 characters in the path component (everything after the current directory's trailing slash) for the google redirect, but not 14. To see what it's actually matching, Try replacing it with this rule:

RewriteRule .+ https://www.google.com/search?q=$0 [B,L,R]

Do you have a previous rule that is missing the L flag, which tells the engine to stop looking for matches?

Upvotes: 1

iikorni
iikorni

Reputation: 517

According to the Apache documentation (https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule):

The syntax of a RewriteRule is RewriteRule Pattern Substitution [flags], where Pattern is a "perl compatible regular expression." This would explain the behaviour you have observed!

Upvotes: 0

Related Questions