我想重写HTTP get请求。请求看起来像这样
example/index.php?opt=1&cat=1
我在.htaccess上使用了这个规则
Options +FollowSymLinks
RewriteEngine on
RewriteRule /(.*)/(.*) index.php?opt=$1&cat=$2
它工作正常。
因此,当我输入example/index/1/1
时,它会显示在页面example/index.php?opt=1&cat=1
上
当我输入example/index/1/2
时,它会显示在页面example/index.php?opt=1&cat=2
上。
但是当我点击example/index/1/3
时,它不会继续example/index.php?opt=1&cat=3
。我怎样才能重写规则。是否有可能在同一个项目上重写多个HTTP get请求,因为我有这样的事情example/demo.php?par1=x&par2=y
答案 0 :(得分:1)
如上面的评论所述,第三个示例url指向索引脚本的问题可能是一个缓存问题。尝试进行深度重新加载或清除浏览器缓存。
对于处理类似模式的更一般的重写规则:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/example/([^/]+)/
RewriteRule /(.*)/(.*) %1.php?opt=$1&cat=$2
但请注意,您在RewriteRule中使用的正则表达式不健壮:根据传入请求的结构,您可能会遇到意外行为。我建议使用类似的东西,假设你将.htaccess样式文件放在DocumentRoot中:
RewriteEngine on
RewriteRule ^example/([^/]+)/([^/]+)/([^/]+)/? $1.php?opt=$2&cat=$3 [QSA]
最后并非最不重要的一些注意事项:如果你可以控制http服务器配置,你应该总是喜欢将这些规则放在主机配置中,而不是使用.htaccess
样式文件。这些文件是非常严重的错误,使事情变得复杂,难以调试并且真的减慢了服务器速度。只有当不控制服务器配置或应用程序需要对设置进行动态更改时,它们才有意义。