有没有更好的方法来执行此正则表达式?

发布于 2024-08-05 05:17:09 字数 1194 浏览 10 评论 0原文

我终于找到了一种好/简单的方法,可以在我的网站上使用正则表达式以下面的格式创建干净的 URL,但是它需要一个非常大的 .htaccess 文件,我从这里的帖子中知道它应该不会太糟糕使用 mod_rewrite 的性能,但我从未真正见过它像我这样使用,几乎我网站的每个页面都有一个单独的条目。

下面是一个条目的示例,1 页有 2 个条目,第一个条目重写

http://www.example.com/users/online/friends/

http://www.example.com/index.php ?p=users.online.friends

它工作得很好,但如果用户不在第一页上,那么 URL 中会添加另一个内容用于分页,我必须编写另一个条目来重写发生这种情况,这是正确的方法还是应该将它们以某种方式结合起来?

RewriteRule ^users/online/friends/*$ ./index.php?p=users.online.friends&s=8
RewriteRule ^users/online/friends/(\d+)/*$ ./index.php?p=users.online.friends&s=8&page=$1

第二个会这样做

http://www.example.com/用户/在线/朋友/22/

http://www.example.com /index.php?p=users.online.friends&page=22

I finally figured out a good/easy way to make clean URLs with regex on my site in this format below, however it will require a very large .htaccess file, I know from the post on here that it is supposed to not be to bad on performance to use mod_rewrite but I have never really seen it used where the way I am, with a seperate entry for almost every page of my site.

Below is an example of an entry, there is 2 entries for 1 page, the first entry re-writes

http://www.example.com/users/online/friends/
to
http://www.example.com/index.php?p=users.online.friends

It works great but if the user is not on the first page then there is another thing added to the URL for paging and I had to write another entry to rewrite when this happens, is this the correct way or should these be combined somehow?

RewriteRule ^users/online/friends/*$ ./index.php?p=users.online.friends&s=8
RewriteRule ^users/online/friends/(\d+)/*$ ./index.php?p=users.online.friends&s=8&page=$1

The second one would do this

http://www.example.com/users/online/friends/22/
to
http://www.example.com/index.php?p=users.online.friends&page=22

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

·深蓝 2024-08-12 05:17:09

这取决于您认为更具可读性,但以下是您如何使用单个规则来做到这一点:(

RewriteRule ^users/online/friends(/(\d+))?/*$ ./index.php?p=users.online.friends&s=8&page=$2

编辑以更忠实于原始问题中尾随斜杠的处理。是:RewriteRule ^ users/online/friends/((\d+)/*)?$ ./index.php?p=users.online.friends&s=8&page=$2)

这里我刚刚输入了“( ……)?”围绕 url 的最后部分使其成为可选匹配,并将反向引用更改为 $2。

当然,这实际上重写了 http://www.domain.com/users/online/friends / 为:

http:// /www.domain.com/index.php?p=users.online.friends&page=

因此,您的 PHP 代码必须检查 page 参数是否非空。

It depends what you think is more readable, but here's how you could do it with a single rule:

RewriteRule ^users/online/friends(/(\d+))?/*$ ./index.php?p=users.online.friends&s=8&page=$2

(Edited to be more faithful to treatment of trailing slash in original question. Was: RewriteRule ^users/online/friends/((\d+)/*)?$ ./index.php?p=users.online.friends&s=8&page=$2)

Here I've just put "(...)?" around the final part of the url to make it an optional match, and changed the backreference to $2.

Of course, this actually rewrites http://www.domain.com/users/online/friends/ as:

http://www.domain.com/index.php?p=users.online.friends&page=

So your PHP code would have to check whether the page parameter is non-empty.

牵你的手,一向走下去 2024-08-12 05:17:09

是的,没关系。我想它们可以合并成一个规则,但实际上没有任何必要。

您可能会考虑将页面作为 URL 的一部分,而不是:

http://www.domain.com/users/online/friends/22/

只使用:,

http://www.domain.com/users/online/friends?page=22

然后制定一个类似以下的规则:

RewriteRule ^users/online/friends/?$ ./index.php?p=users.online.friends&s=8 [L,QSA]

附加查询字符串

编辑: 有几种方法可以减少重写你的规则。

首先,在搜索词中使用通配符,例如:

RewriteRule ^users/(\w+)/(\w+)$ /index.php?p=users.$1.$2 [L,QSA]

会减少相当多的规则。

其次,如果您通过 /index.php 传递所有内容,只需考虑将所有请求委派到那里:

RewriteRule ^(users/*)$ /index.php/$1 [L,QSA]

该规则使用第三种技术:不通过查询字符串参数传递路径信息,而是通过额外的路径信息传递路径信息。可以通过 $_SERVER['PATH_INFO'] 访问。

话虽这么说,很多规则并不一定是坏事。至少你的所有行为都是明确的。然而,您必须注意的是造成维护噩梦。

Yes, that's fine. I guess they could be combined into a single rule but there's not really any need.

You might consider leaving page as part of the URL so instead of:

http://www.domain.com/users/online/friends/22/

just have:

http://www.domain.com/users/online/friends?page=22

and then have one rule something like:

RewriteRule ^users/online/friends/?$ ./index.php?p=users.online.friends&s=8 [L,QSA]

to append the query string

Edit: There are a couple of ways of reducing the number of rewrite rules you have.

Firstly, use wildcards in the search terms, like:

RewriteRule ^users/(\w+)/(\w+)$ /index.php?p=users.$1.$2 [L,QSA]

will reduce quite a number of rules.

Secondly, if you're passing everything through /index.php just consider delegating all requests there:

RewriteRule ^(users/*)$ /index.php/$1 [L,QSA]

That rule uses a third technique: instead of passing the path information via a query string parameter, pass it via the extra path info. That can be accessed via $_SERVER['PATH_INFO'].

That being said, lots of rules isn't necessarily bad. At least it's explicit about all your actions. The thing you have to watch out for is creating a maintenance nightmare however.

后来的我们 2024-08-12 05:17:09
# Initial step
RewriteCond %{QUERY_STRING} !(?:^|&)p=
RewriteRule ^([^/]+)/(.+) /$2?p=$1 [QSA]

# Subsequent steps
RewriteCond %{QUERY_STRING} ((?:[^&]*&)*?)p=([^&]*)(.*)
RewriteRule ^([^/]+)/(.+) /$2?%1p=%2.$1%3

# Last step with page number
RewriteRule ^(\d+)/?$ /index.php?page=$1 [QSA,L]

# Last step without page number
RewriteCond %{QUERY_STRING} (?:((?:[^&]*&)*?)p=([^&]*))?(.*)
RewriteRule ^([^/]+)/?$ /index.php?%1p=%2.$1%3 [L]

这将分几个步骤重写 URL:

  1. http://www.domain.com/users/online/friends/22/
  2. http://www.domain.com/online/friends /22/?p=users
  3. http://www.domain.com/friends/22/?p=users.online
  4. http://www.domain.com /22/?p=users.online.friends
  5. http://www.domain.com/index.php?p=users.online.friends&page=22

更简单的方法如下所示,但需要您更改脚本:

RewriteRule ^(.*?)(?:/(\d+))?/?$ /index.php?p=$1&page=$2 [QSA,L]

它将一步完成所有操作,但略有不同:

  1. http://www.domain.com/users/online/friends/22/
  2. http://www.domain.com/index.php?p=users/online/friends&page=22

添加 s=8 查询参数需要更多工作:

  1. 创建一个文本文件,其中包含每页的菜单编号。
  2. 添加 RewriteMap 指令。
  3. 更改倒数第二个规则以使用与最后一个规则相同的 RewriteCond
  4. &s=%{menumap:%2|0} 添加到最后两条规则。
# Initial step
RewriteCond %{QUERY_STRING} !(?:^|&)p=
RewriteRule ^([^/]+)/(.+) /$2?p=$1 [QSA]

# Subsequent steps
RewriteCond %{QUERY_STRING} ((?:[^&]*&)*?)p=([^&]*)(.*)
RewriteRule ^([^/]+)/(.+) /$2?%1p=%2.$1%3

# Last step with page number
RewriteRule ^(\d+)/?$ /index.php?page=$1 [QSA,L]

# Last step without page number
RewriteCond %{QUERY_STRING} (?:((?:[^&]*&)*?)p=([^&]*))?(.*)
RewriteRule ^([^/]+)/?$ /index.php?%1p=%2.$1%3 [L]

This would rewrite the URL in several steps:

  1. http://www.domain.com/users/online/friends/22/
  2. http://www.domain.com/online/friends/22/?p=users
  3. http://www.domain.com/friends/22/?p=users.online
  4. http://www.domain.com/22/?p=users.online.friends
  5. http://www.domain.com/index.php?p=users.online.friends&page=22

An easier method would be the following, but would require you to change your scripts:

RewriteRule ^(.*?)(?:/(\d+))?/?$ /index.php?p=$1&page=$2 [QSA,L]

It would do everything in one step, with a little difference:

  1. http://www.domain.com/users/online/friends/22/
  2. http://www.domain.com/index.php?p=users/online/friends&page=22

Adding the s=8 query argument would require more work:

  1. Creating a text-file with the menu numbers for each page.
  2. Adding a RewriteMap directive.
  3. Changing the second-last rule to use the same RewriteCond as the last rule has.
  4. Adding &s=%{menumap:%2|0} to the last two rules.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文