.htaccess RewriteRule 问题

.htaccess RewriteRule 问题

在提问之前,让我告诉你一些假设,我的网络服务器上有 5 个文件 index.php config.php read.php write.php .htaccess

我在 .htaccess 中写了以下 URL 重写规则

RewriteEngine 在 RewriteRule 上 ^(\w+)$ read.php?id=$1

现在,当我输入 domain.com/xyz 时,它会从 read.php?id=xyz 获取数据,这很好:)

但是当我输入 domain.com/index 时,它会从 index.php 获取数据,或者当我输入 domain.com/write 或 domain.com/config 或 domain.com/read 时,它会分别从 write.php 、 config.php 和 read.php 获取数据

我想要从 read.php?id=index 或 read.php?id=config 或 read.php?id=read 或 read.php?id=write 中获取数据

有人能帮助我吗?

抱歉我的英语不好

答案1

Options -multiviews

由于某种原因,您的原始规则必须有一个 RewriteCond 来检查文件是否存在。由于多视图已打开,并且 index.php 确实存在,并且您已请求索引,因此 mod_negotiation 将假定您想要获取 index.php

当多视图关闭时,您的规则仍能正常工作。

答案2

尝试这个.....

RewriteEngine 在 RewriteRule 上 ^([a-zA-Z0-9_-]+)/$ read.php?id=$1

答案3

使用 RewriteRule ^(.*)$ read.php?id=$1 [QSA,L] 这样,所有请求都会被重写

相关内容