URL重写GET参数 [英] URL Rewrite GET parameters

查看:419
本文介绍了URL重写GET参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的网址如下所示

I want my url to look like the following

www.website.com/home&foo=bar&hello=world

我只想要第一个GET参数更改

I only want the first get parameter to change

然而实际的幕后的网址就是这个

However the actual "behind the scenes" url is this

www.website.com/index.php?page=home&foo=bar&hello=world

所有教程我发现改变所有的参数。

All tutorials I find change all of the parameters.

任何帮助非常AP preciated!

Any help much appreciated!

推荐答案

添加到您的的.htaccess 在Web根 / 目录

Add this to your .htaccess in your web root / directory

RewriteEngine on
RewriteBase /

RewriteRule ^home$ index.php?page=home&%{QUERY_STRING} [NC,L]

如果你想要这个工作的所有网页,即 /任何页面被担任的index.php?页=所有页然后用

If you want this to work for all pages i.e. /any-page gets served as index.php?page=any-page then use

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d # not a dir
RewriteCond %{REQUEST_FILENAME} !-f # not a file
RewriteRule ^(.*)$ index.php?page=$1&%{QUERY_STRING} [NC,L]


如何将这些规则的工作?


How do these rules work?

A 重写规则语法如下

RewriteRule [Pattern] [Substitution] [Flags]

模式的可使用普通的前pression并且匹配的主机名和端口(与的.htaccess 放在根目录),但任何查询字符串之前。

The Pattern can use a regular expression and is matched against the part of the URL after the hostname and port (with the .htaccess placed in the root dir), but before any query string.

该模式 ^家$ 使得第一条规则匹配传入的URL www.website.com/home 。该%{QUERY_STRING} 简单的捕捉和后追加什么的/ home?的内部取代网址的index.php?页=家里

The pattern ^home$ makes the first rule match the incoming URL www.website.com/home. The %{QUERY_STRING} simply captures and appends anything after /home? to the internally substituted URL index.php?page=home.

标志 NC 只是使规则不区分大小写的,因此它匹配 /首页 / HOME 为好。而简单地将其标记为去年即改写的情况下有如下定义的任何其他规则应该到此为止。

The flag NC simply makes the rule non case-sensitive, so that it matches /Home or /HOME as well. And L simply marks it as last i.e. rewriting should stop here in case there are any other rules defined below.

这一次只是比较通用的,即如果所有的网站页面的编写遵循一些规则,每个页面的这个URL模式则相反,我们可以只使用这个通用的。

This one's just more generic i.e. if all of your site pages follow this URL pattern then instead of writing several rules, one for each page, we could just use this generic one.

。* 模式中的 ^(。*)$ 匹配 /任何-page名和括号有助于捕捉的任何页面名称部分为 $ 1 在替换URL作为的index.php?page变量= $ 1 。该&安培; 页=家庭及放大器; 1 页= $&安培; 简直是多个查询字符串字段值对之间使用的分隔符。

The .* in the pattern ^(.*)$ matches /any-page-name and the parentheses help capture the any-page-name part as a $1 variable used in the substitution URL as index.php?page=$1. The & in page=home& and page=$1& is simply the separator used between multiple query string field-value pairs.

最后,%{QUERY_STRING} [NC,L] 标记的工作方式相同的规则之一。

Finally, the %{QUERY_STRING} and the [NC,L] flags work the same as in rule one.

这篇关于URL重写GET参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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