简体   繁体   English

将所有内容重定向到一个文件,禁止直接访问PHP文件

[英]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. 我正在努力在.htaccess文件中创建一组规则,导致出现500个内部服务器错误。

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: 我希望我的.htaccess行为如下:

  1. All requests (except resources, images, CSS, js, etc) should redirect to /router.php, preserving query strings. 所有请求(资源,图像,CSS,js等除外)都应重定向到/router.php,并保留查询字符串。 I don't need the REQUEST_URI as a query string as it is available via $_SERVER['REQUEST_URI'] regardless. 我不需要REQUEST_URI作为查询字符串,因为无论如何它都可以通过$_SERVER['REQUEST_URI']

  2. All attempts to access a PHP file directly, for example user enters "index.php", should also redirect to router.php. 所有直接访问PHP文件的尝试,例如用户输入“ index.php”,也应重定向到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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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