Stoopkid
Stoopkid

Reputation: 1955

Apache rewrite conditions not catching existing directories or files

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/board/([a-zA-Z0-9\_\-]*).* /board/index.php?$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/board/user/([a-zA-Z0-9\_\-]*).* /board/user/index.php?$1 [L]

I am trying to forward mydomain.com/board/something to mydomain.com/board/index.php?something and /board/user/something to /board/user/index.php?something. And have it not rewrite existing files or directories.

The board/ one works but it fires even if i request an existing directory so I can't test the second one. Also a page like mydomain.com/board/signup/index.php is forwarding to /board/index.php?signup

These are all within a <VirtualHost *:443> and my VPS is actually handling a few different domain names on the same instance if that makes a difference.

I found similar issues in search but their solution was what I am using above already.

Upvotes: 0

Views: 38

Answers (1)

Stoopkid
Stoopkid

Reputation: 1955

Apparently on Apache 2.4 (maybe 2.2 and above?) you have to do it like this:

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d

However this does not catch if you navigate to, say, mydomain.com/board/ when there is an index.php there. So this is what I have come up with, finally.

RewriteEngine On

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond $1 !index\.
RewriteCond %{DOCUMENT_ROOT}/board/$1/index.php !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteRule ^/board/([a-zA-Z0-9\_\-\.]*) /board/index.php?$1 [L]

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond $1 !index\.
RewriteCond %{DOCUMENT_ROOT}/board/user/$1/index.php !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteRule ^/board/user/([a-zA-Z0-9\_\-\.]*) /board/user/index.php?$1 [L]

Upvotes: 1

Related Questions