htaccess的mod-rewrite规则和&符号 [英] .htaccess mod rewriterule and ampersands

查看:178
本文介绍了htaccess的mod-rewrite规则和&符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力奋斗着这个两天现在。

目前,我有以下重写规则:

 重写规则^(。*)$的index.php?U = $ 1 [NC,L]
 

哪些端口后面example.com/一切的index.php作为GET变量u。这是正在处理的index.php与爆炸定义TE不同的参数是:市(第一个斜线之间)连接的位置(第二个参数)

这完全适用于以下网址:

example.com/City/location/或example.com/City/location
example.com/City/或example.com/City

主要生产:

  $ ARG [0] =城市
$ ARG [1] =位置
 

问题发生时的位置有一个与符号的位置名称:

  example.com/city/location&more
 

语句:

 的index.php U =城市/位置和放大器;更
 

现在的问题是显而易见的;我不能在index.php中使用的爆炸参数了,因为位置名称的第二部分是不存在$ _GET ['U']。

我怎样才能解决这个问题?

我觉得三个解决方案,我不知道如何实现:
1)重写重写规则分裂的的.htaccess
的城市和地点 2)使用urlen code中的.htaccess相当于改造和放大器; -sign至26%
3)一个完整的解决方案,其他的:)
感谢名单提前!

修改 首先: 当链路正由我的网站产生它被转换与urlen code。所以有不&安培;其中不应该有1。 在与放大器;是业务的一部分namen,如:百吉饼和放大器;豆 当人们想搜索的公司,他们键入:在URL字段www.example.com/city/bagels&beans。 这就是问题的开始。

修改2 当我去:example.com/city/bagels%26Beans 它转换为以下:$ _ GET:

 阵列
(
    [U] =>城市/贝果
    [豆类] =>
)
 

修改3 我的方式引爆$ _GET ['U'],形成的论点。

  $ ARG =爆炸('/',$ _ GET ['U']);
 

解决方案

您并不需要把URL路径的争论。你可以简单地解析原来要求的路径是这样的:

  $ _ SERVER ['REQUEST_URI_PATH'] =的strtok($ _ SERVER ['REQUEST_URI']'?');
$段=爆炸('/',修剪($ _ SERVER ['REQUEST_URI_PATH'],'/'));
 

现在你需要的是这个规则重写请求您的的index.php 的:

 的RewriteCond%{} REQUEST_FILENAME!-f
重写规则!^指​​数\ .PHP $的index.php [L]
 

的附加条件是只重写不能被映射到已存在的文件的请求。

I've been struggling with this for two days right now.

At the moment I have the following rewrite rule:

RewriteRule ^(.*)$ index.php?u=$1 [NC,L]

Which ports everything behind example.com/ to index.php as the GET variable u. Which is being processed in index.php with an explode to define te different arguments being: the city (between the first slashes) en the location (the second argument)

THis works perfectly for the following urls:

example.com/City/location/ or example.com/City/location
example.com/City/ or example.com/City

Which produces:

$arg[0] = city
$arg[1] = location

The problem happens when the location has an Ampersand in the location name:

example.com/city/location&more

translates to:

index.php?u=city/location&more

The problem is obvious; i can't use the explode parameter in index.php anymore because the second part of the location name is not stored in $_GET['u'].

How can i solve this?

I think of three solutions which i don't know how to implement:
1) rewriting the rewrite rule to split the city and location in the .htaccess
2) using a .htaccess equivalent of urlencode to transform the &-sign to %26
3) a complete other solution:)
Thanx in advance!

Edit Firstly: When the link is being produced by my website it gets translated with urlencode. So there isn't a & where there shouldn't be one. The & is part of a business namen like: "Bagels&Beans" When people want to search that company they type: www.example.com/city/bagels&beans in the url-field. ANd that is where the problem starts.

edit 2 When i go to: example.com/city/bagels%26Beans it translates to the following: $_GET:

Array
(
    [u] => city/Bagels
    [Beans] => 
)

edit 3 The way i explode the $_GET['u'] to form the arguments.

$arg = explode('/',$_GET['u']);

解决方案

You don’t need to put the URL path in an argument. You can simply parse the original requested path like this:

$_SERVER['REQUEST_URI_PATH'] = strtok($_SERVER['REQUEST_URI'], '?');
$segments = explode('/', trim($_SERVER['REQUEST_URI_PATH'], '/'));

Now all you need is this rule to rewrite the request to your index.php:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^index\.php$ index.php [L]

The additional condition is to only rewrite requests that can not be mapped to existing files.

这篇关于htaccess的mod-rewrite规则和&符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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