简体   繁体   English

url为lighttpd重写-正则表达式

[英]url rewrite for lighttpd - regex

I would like to convert my urls to short ones without get parameters but for the first try I would reduce the regex to only one case: 我想将我的网址转换为不带参数的短网址,但第一次尝试,我会将正则表达式减少为仅一种情况:

There are many normal urls that should not be affected. 有许多正常的URL不应受到影响。 The only case I want to rewrite the urls is the user profile page. 我要重写网址的唯一情况是用户个人资料页面。

Currently they look like this: 目前,它们看起来像这样:

www.dummy.com/index.php?user=USERNAME&id=USERID

Target scheme looks like this: 目标方案如下所示:

www.dummy.com/USERNAME/USERID

The usernames can contain mostly any character (without /) not just az/AZ. 用户名几乎可以包含任何字符(不带/),而不仅仅是z / AZ。

I was never really good at regexing and have no clue how to handle this problem. 我从来没有真正擅长过正则表达式,也不知道如何处理这个问题。 Any suggestions are welcome. 欢迎任何建议。

There won't be requests to a folder (www.dummy.com/folder/subfolder) but confirming that the userid is an integer value would be nice but is not necessary. 不会有对文件夹的请求(www.dummy.com/folder/subfolder),但是确认userid是一个整数值会很好,但不是必须的。

You can use this code in your DOCUMENT_ROOT/.htaccess file: 您可以在DOCUMENT_ROOT/.htaccess文件中使用以下代码:

RewriteEngine On
RewriteBase /

RewriteRule ^(\w+)/(\d+)/?$ index.php?user=$1&id=$2 [L,QSA]

This will match either one of these URLs: 这将与以下网址之一匹配:

/abcd/123
/abcd/123/
/abcd456/6789
/abcd456/6789/

EDIT: 编辑:

In LigthHttpd use this equivalent rule: 在LigthHttpd中,请使用以下等效规则:

url.rewrite = ( "^/(\w+)/(\d+)/?(?:\?(.*))?$" => "/index.php?user=$1&id=$2&$3" )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM