0

I have two rules that currently function as they should, but I feel like they can be consolidated into one for the benefit of a smaller config to maintain and less rules to process for each HTTP request. The rules:

RewriteRule ^/user/[0-9]+/?$ /index.php [NC,QSA,L] 
RewriteRule ^/user/[0-9]+/(.*)$ /$1 [NC,QSA,L]

I had to add the first rule with an explicit redirect to /index.php because going to http://www.example.com/user/5/, for example, would give me an HTTP 400. The rewrite would internally rewrite to /, but instead of reading the DirectoryIndex, index.php, the error_log would give me: client denied by server configuration: /.

Adding the explicit redirect to /index.php fixed URLs such as http://www.example.com/user/5/ and the second rule would work for everything else.

Is there a way to set the config to just normally use the DirectoryIndex I have set and not explicitly list index.php here? http://www.example.com works just fine without the rewrite rules, so I'm not entirely sure why a rewrite would break it.

1
  • Those really should be two rules. Of course, it's better still if your web application uses a front controller. Jan 3, 2015 at 23:08

1 Answer 1

1

You can combine these 2 rules. I think these rules are located in a VHost config file or another config file, but not in .htaccess, nor in a Directory section inside the VHost config. This means you are in a per server context.

The problem:

In per server context, the substitution (last part of the rewrite) can be a file system path or an URL.

Apache tries to guess if you meant an URL or a file system path. The RewriteRule doc states:

Substitutions are only treated as a file-system path when the rule is configured in server (virtualhost) context and the first component of the path in the substitution is exists in the file-system

So your rule nr 2 rewrites everything that begins with /user/[0-9]/ to /. And now you can see that the path / (the path with no name) exists in the file system. But of course you have restricted access to that directory, so you have the client denied.

The solutions:

Remove the first rule, with the index.php and

Use the PT flag (passthrough):

RewriteEngine On
RewriteRule ^/user/[0-9]+/(.*)$ /$1 [NC,QSA,L,PT]

Or be more specific in your substitution:

RewriteEngine On
RewriteRule ^/user/[0-9]+/(.*)$ /root/and/full/path/to/www/$1 [NC,QSA,L]

Hope this helps!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .