简体   繁体   中英

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.

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM