Php 如何向重写引擎添加许多规则
您好,我有一个重写规则示例,它生成以下示例:Php 如何向重写引擎添加许多规则,php,apache,.htaccess,mod-rewrite,Php,Apache,.htaccess,Mod Rewrite,您好,我有一个重写规则示例,它生成以下示例: [http://localhost/project/index.php?url=something] 在我的网站上,我用它来制作 [http://localhost/project/index.php?url=task/add] 显示为 [http://localhost/project/task/add] 这是我的密码 RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond
[http://localhost/project/index.php?url=something]
在我的网站上,我用它来制作
[http://localhost/project/index.php?url=task/add]
显示为
[http://localhost/project/task/add]
这是我的密码
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
我还需要做三件事
[http://localhost/project/index.php?url=tasks/view/&page=2]
显示为[http://localhost/project/tasks/view:2]
或[http://localhost/project/tasks/view#2]
首先,我想提醒您,
#
是url的“锚定”部分。它从未发送到服务器,因此重写它可能不是最好的主意
现有规则所做的是在内部重写所有未映射到现有文件、目录或index.php符号链接的请求。如果您想重写像http://localhost/project/tasks/view:2
将被重写为http://localhost/project/index.php?url=tasks/view&page=2
,您需要在已有规则之前添加此规则。否则,更一般的规则会在更具体的规则出现之前匹配它
我还假设您在/project/
目录中有.htaccess
生成url
[http://localhost/project/index.php?url=tasks/view/&page=2]
显示为[http://localhost/project/tasks/view:2]
或[http://localhost/project/tasks/view#2]
在现有规则之前添加以下规则可以很好地处理这些URL
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^tasks/view:(.+)$ index.php?url=tasks/view&page=$1 [QSA,L]
或者,你为什么不干脆explode($_GET['url'],':')用index.php
中的code>代替此规则
然后我的上传文件夹命名为(文件),我想重定向任何人得到任何
此文件夹中的文件仅适用于浏览器
如果要停止对/project/files
的所有直接请求,可以使用%{the_REQUEST}
技巧和[F]
(禁止)标志。如果要显示自定义错误页面,请为禁止状态代码添加一个
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /project/files
RewriteRule ^ - [F,L]
在名为(files)的上载文件夹上下载文件,自定义url如下
[http://localhost/project/getfile/1]
请记住,对于发送到服务器的每个请求,Apache都会将其与RewriteRule中的正则表达式进行匹配,并在内部重写它或重定向用户。要在内部将对/project/getfile/1
的请求重写为/project/files/1
,可以使用以下规则。将其添加到已有规则之前
RewriteRule ^getfile/(.*)$ /files/$1 [L]
我鼓励您阅读mod_rewrite。谢谢兄弟,但我正在开发自定义框架,所以首先我需要像http://localhost/project/$url:$page
beenhttp://localhost/project/index.php?url=$url&page=$page
然后是名为(文件)的我的上载文件夹我想将任何人重定向到错误页面以从此文件夹中无法获取任何文件你好,我使用的兄弟RewriteRule^files/-[R=404,L,NC]
cuzRewriteCond%{u请求}^(get | POST)\/files RewriteRule^-[F,L]
不起作用,但它隐藏了我需要的一切,只显示在我的sript页面上RewriteRule^files/-[R=404,L,NC]
将导致/getfile/…
url现在也产生404错误。如果/files
文件夹不在项目
文件夹中,则需要在www根目录中创建.htaccess
,并将规则添加到该.htaccess
文件夹中。