1

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.

1 Answer 1

1

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] 
Sign up to request clarification or add additional context in comments.

10 Comments

That works, but it still goes to a certain PHP file in case it exists. For example, if I enter /test.php (an existing file) it loads that file, not router.php
Really? It shouldn't happen and I had tested that condition explicitly before posting. But I will break this rule into 2 to force it.
Thanks, but this now gives me a 500 Internal Error, regardless of URL I load )=
That is really weird because I tested it again and found it working. Do you have other rules in your .htaccess OR any other .htaccess in your system.
Also let me know if you're using any CMS/PHP framework
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.