Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex .htaccess:清除url的参数url_Regex_.htaccess_Mod Rewrite - Fatal编程技术网

Regex .htaccess:清除url的参数url

Regex .htaccess:清除url的参数url,regex,.htaccess,mod-rewrite,Regex,.htaccess,Mod Rewrite,我正在尝试将一个带有参数的丑陋url转换成一个漂亮的url。目前我有: 我希望这样: 我已尝试使用此。htaccess: RewriteEngine On RewriteRule ^/?$/?$ index.php?reg=$1&area=london&id=$2 [L,QSA] 这是我从一个在线生成器得到的,但是当我在url中运行带有/uk/16的页面时,它就崩溃了 我做错了什么 下面是对Chris回复的回复。所有这些都是可选的 url的结构如下所示: myasite.c

我正在尝试将一个带有参数的丑陋url转换成一个漂亮的url。目前我有:

我希望这样:

我已尝试使用此。htaccess:

RewriteEngine On
RewriteRule ^/?$/?$ index.php?reg=$1&area=london&id=$2 [L,QSA]
这是我从一个在线生成器得到的,但是当我在url中运行带有/uk/16的页面时,它就崩溃了

我做错了什么

下面是对Chris回复的回复。所有这些都是可选的

url的结构如下所示:

myasite.com
myasite.com/uk (if set, This will always be text and always 2 chars long)
myasite.com/uk/london (if set, This will always be text, this will be any char length )
myasite.com/uk/london/16 (if set, This will always be integer and any char length)

我们正在将
$1/$2/
$1/$2
重写为
index.php?reg=$1&id=$2
您的正则表达式不正确。您的
^/?$/?$
表示请求只能有2个
/
s,每个都是可选的。您也没有使用任何捕获组,因此
$1
$2
没有上下文。下面是一个适用于您提供的示例的正则表达式:

^/(uk)/(\d+)$
如果
uk
可以是您可以使用的任意2个小写字母:

^/([a-z]{2})/(\d+)$
您可以使用regex101查看regexs的功能

(你的规则)

页面右侧给出了解释

作为重写规则:

RewriteEngine On
RewriteRule ^/([a-z]{2})/(\d+)$ index.php?reg=$1&id=$2 [L,QSA]

您只需在
.htaccess
文件中使用以下内容:

RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /index.php?reg=$1&area=$2&id=$3 [L]

这将为您留下所需的URL:
http://myasite.com/uk/london/16
。请确保在测试之前清除缓存。

无论是谁执行了-1操作,请发表评论!谢谢,我会试试
reg
id
的可接受值是多少?Id可能只是一个整数?你做错的是正则表达式
/?$/?
说可以有两个可选的
/
s,没有别的。@chris85我在写q时犯了一个小错误-请看新的post@chris85是的,我也这么想。但老实说,我对这件事有点不知所措。不知道我在做什么。我需要这三个都工作,但如果一个没有设置,也不能崩溃。你能帮忙吗?根据你的更新,你需要有3条规则。请使用regex101链接,如果您对regexs有疑问,请提问。如果我没有设置最后两个参数,我会得到404。我试着用()把每一个都包起来?但这不起作用。该死的,我很烂。哦,我忘了整理城市/地区了。“目录”2的周围应该有
()
。所以
RewriteRule^/([a-z]{2})/([a-z]+?)/(\d+$index.php?reg=$1&id=$3&area=$2[L,QSA]
我想我会为
http://myasite.com/uk/london/16
。我在任何舞台上都能得到404分/uk和/uk/london和uk/london/16。有什么想法吗?你有重写规则吗?也许
RewriteRule^([a-z]{2})/([a-z]+?)/(\d+$index.php?reg=$1&id=$3&area=$2[L,QSA]
。。。或者将前导的
/
设置为可选。。
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /index.php?reg=$1&area=$2&id=$3 [L]