.htaccess 将查询字符串重写为路径 [英] .htaccess rewrite query string as path

查看:19
本文介绍了.htaccess 将查询字符串重写为路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了这个问题,但我只遇到了似乎难以根据我的特定需求量身定制的非常具体的答案.

I've searched for this question but I only come across really specific answers that seem difficult to tailor to my specific needs.

假设我试图重写的 URL 是这样的:

Let's say the URL I'm attempting to rewrite is this:

http://www.example.org/test.php?whatever=something

我想重写它,使它看起来像这样:

I want to rewrite it so that it appears as this:

http://www.example.org/test/something

我该怎么做?

推荐答案

为了将像 /test/something 这样的请求路由到内部重写,以便 /test.php 中的内容?whatever=something 得到服务,您将在文档根目录的 htaccess 文件中使用这些规则:

In order to route a request like /test/something to internally rewrite so that the content at /test.php?whatever=something gets served, you would use these rules in the htaccess file in your document root:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?test/(.*?)/?$ /test.php?whatever=$1 [L]

为了将查询字符串 URL 重定向到更好看的 URL:

And in order to redirect the query string URL to the nicer looking one:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /test\.php\?whatever=([^\&\ ]+)
RewriteRule ^/?test\.php$ /test/%1? [L,R=301]

这篇关于.htaccess 将查询字符串重写为路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆