2

I'm trying to figure out what I'm doing wrong with this .htaccess rewrite:

Options -Indexes

RewriteEngine  on
RewriteBase    /test/site/
RewriteOptions MaxRedirects=10

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /test/site/public/page.php?rel=$1 [NC,L,B]

I have an Front end script that handles all of the requests. In my case I don't need or have index.php/index.html file in my root project (/test/site/). That causes Apache to throw 403 error because of the first line of the code.

The code will work only if you access the links as follows:

http://localhost/test/site/public/index
http://localhost/test/site/public/something/else/index.html

But if you enter

http://localhost/test/site/public/

It'll throw 403 error.

I'm pretty sure I'm doing something wrong but I can't figure it out.

migrated from serverfault.com Jun 4 '11 at 2:50

This question came from our site for system and network administrators.

1

Well, your RewriteRule does not apply because that path you are trying is a directory. As set up, your .htaccess redirects things that are neither.

You will need an additional rule or a custom 403 page to handle directories. You may also change your condition to files only. In other words, remove the second RewriteCond line. Which you do depends on what you want to happen for a directory path.

  • In my understanding the configuration above should redirect to page.php whenever a file or directory doesn't exist, right ? Also adding ErrorDocument 403 page.php seems to fix the problem. I'm a bit new to rewriting so I hope I've got it right. – tftd Jun 4 '11 at 8:03
  • @tftd - Exactly. That directory exists, so it does not get redirected. Do not forget to accept the answer if it worked for you. – Itai Jun 4 '11 at 15:21
  • @tftd "adding ErrorDocument 403 page.php seems to fix the problem" - adding that simply masks the problem. It might look like the correct page is served, but the server would still return and log a 403. (But also, legitimate 403s would break and would need to be handled entirely by your framework.) – MrWhite Apr 7 '16 at 14:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.