java url重写带参数,URL重写GET参数

I want my url to look like the following

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

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.

Any help much appreciated!

解决方案

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

RewriteEngine on

RewriteBase /

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

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 has the following syntax

RewriteRule [Pattern] [Substitution] [Flags]

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.

First Rule

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.

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.

Second Rule

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.

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.

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值