Tool
Tool

Reputation: 12488

Why does "RewriteRule (.*)" fails, and RewriteRule .* works?

I've placed this .htaccess in my root folder:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule   (.*) public/$1    [L]
 </IfModule>

It causes a 500 Internal server error with no explanation.

If I remove the paranthesis it works:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule   .* public/$1    [L]
 </IfModule>

Why? Aren't paranthesis supposed to be neutral here?

I'm using the latest XAMPP version (Windows).

Upvotes: 0

Views: 547

Answers (3)

Felipe Alameda A
Felipe Alameda A

Reputation: 11809

Regex groups are enclosed in round brackets and in this case the expression .* matches everything, so the rule is always used.

With the expression in parenthesis, the rule maps to public/WhateverIsInsideParenthesis, because $1 is the back reference to group 1: (.*). The only one in this case.

Without the parenthesis, the rule maps just to public/, because there is no group. Nothing is inside parenthesis.

You don't give an example of the request, but for something like:

http://example.com/folder1/folder2/script1/ the URI-path inside the parenthesis would be folder1/folder2/script1/ and the substitution URL:

http://example.com/public/folder1/folder2/script1/

Therefore, there are several reasons for an error in this case, with or without parenthesis. The request, where the rule is mapping to, loops, etc., all might generate that error. The 500 Internal Server Error is the general HTTP status code used for everything that goes wrong.

Upvotes: 1

Olaf Dietsche
Olaf Dietsche

Reputation: 74078

Yes, the parenthesis are neutral for the pattern.

But they make a difference, when you use the captured string in the substitution as $1. Then the rule is equivalent to either

RewriteRule (.*) public/$1

or

RewriteRule .* public/

When you leave out the parenthesis and replace $1 with $0

RewriteRule .* public/$0

then you have the same error again, this time without parenthesis.

Upvotes: 0

AbsoluteƵER&#216;
AbsoluteƵER&#216;

Reputation: 7880

What happens if you put the start character ^ in?

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteRule   ^(.*) public/$1    [L]
</IfModule>

Upvotes: 0

Related Questions