.htaccess mod rewriterule 和 & 符号 [英] .htaccess mod rewriterule and ampersands

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

问题描述

我已经为此苦苦挣扎了两天.

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]

将 example.com/后面的所有内容移植到 index.php 作为 GET 变量 u.它正在 index.php 中处理,并用一个爆炸来定义不同的参数:城市(在第一个斜杠之间)和位置(第二个参数)

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)

这适用于以下网址:

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

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

产生:

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

当位置名称中包含与号时会出现问题:

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

example.com/city/location&more

翻译成:

index.php?u=city/location&more

问题很明显;我不能再在 index.php 中使用爆炸参数,因为位置名称的第二部分没有存储在 $_GET['u'] 中.

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?

我想到了三个我不知道如何实施的解决方案:
1)在.htaccess
中重写rewrite规则来拆分城市和位置2) 使用 .htaccess 相当于 urlencode 将 &-sign 转换为 %26
3) 一个完整的其他解决方案:)
提前谢谢!

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!

编辑首先:当我的网站生成链接时,它会使用 urlencode 进行翻译.所以没有 &不应该有的地方.&是企业名称的一部分,例如:Bagels&Beans"当人们想要搜索该公司时,他们在 url 字段中输入:www.example.com/city/bagels&beans.而这就是问题的开始.

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.

编辑 2当我去:example.com/city/bagels%26Beans它转换为以下内容:$_GET:

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

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

编辑 3我分解 $_GET['u'] 以形成参数的方式.

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

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

推荐答案

您不需要将 URL 路径放在参数中.您可以像这样简单地解析原始请求的路径:

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'], '/'));

现在您需要的只是将请求重写到您的index.php:

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 rewriterule 和 & 符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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