silkfire
silkfire

Reputation: 25945

Redirect everything to one file, disallow direct access to PHP files

I'm struggling with creating a set of rules in my .htaccess file, resulting in constant 500 Internal Server Errors instead.

RewriteRule ^(.*)\.php$ /router.php?rq=$1

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /router.php?rq=$1 [L,QSA]

I want my .htaccess to behave like this:

  1. All requests (except resources, images, CSS, js, etc) should redirect to /router.php, preserving query strings. I don't need the REQUEST_URI as a query string as it is available via $_SERVER['REQUEST_URI'] regardless.

  2. All attempts to access a PHP file directly, for example user enters "index.php", should also redirect to router.php.

Upvotes: 1

Views: 160

Answers (1)

anubhava
anubhava

Reputation: 785128

You can use this rule for your routing:

RewriteCond %{REQUEST_URI} !/router\.php [NC]
RewriteRule \.php$ /router.php [NC,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /router.php [NC,L]

Upvotes: 1

Related Questions