有没有更好的方法来执行此正则表达式?
我终于找到了一种好/简单的方法,可以在我的网站上使用正则表达式以下面的格式创建干净的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这取决于您认为更具可读性,但以下是您如何使用单个规则来做到这一点:(
编辑以更忠实于原始问题中尾随斜杠的处理。是:
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:
(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.
是的,没关系。我想它们可以合并成一个规则,但实际上没有任何必要。
您可能会考虑将页面作为 URL 的一部分,而不是:
只使用:,
然后制定一个类似以下的规则:
附加查询字符串
编辑: 有几种方法可以减少重写你的规则。
首先,在搜索词中使用通配符,例如:
会减少相当多的规则。
其次,如果您通过 /index.php 传递所有内容,只需考虑将所有请求委派到那里:
该规则使用第三种技术:不通过查询字符串参数传递路径信息,而是通过额外的路径信息传递路径信息。可以通过
$_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:
just have:
and then have one rule something like:
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:
will reduce quite a number of rules.
Secondly, if you're passing everything through /index.php just consider delegating all requests there:
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.
这将分几个步骤重写 URL:
http://www.domain.com/users/online/friends/22/
http://www.domain.com/online/friends /22/?p=users
http://www.domain.com/friends/22/?p=users.online
http://www.domain.com /22/?p=users.online.friends
http://www.domain.com/index.php?p=users.online.friends&page=22
更简单的方法如下所示,但需要您更改脚本:
它将一步完成所有操作,但略有不同:
http://www.domain.com/users/online/friends/22/
http://www.domain.com/index.php?p=users/online/friends&page=22
添加
s=8
查询参数需要更多工作:RewriteMap
指令。RewriteCond
。&s=%{menumap:%2|0}
添加到最后两条规则。This would rewrite the URL in several steps:
http://www.domain.com/users/online/friends/22/
http://www.domain.com/online/friends/22/?p=users
http://www.domain.com/friends/22/?p=users.online
http://www.domain.com/22/?p=users.online.friends
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:
It would do everything in one step, with a little difference:
http://www.domain.com/users/online/friends/22/
http://www.domain.com/index.php?p=users/online/friends&page=22
Adding the
s=8
query argument would require more work:RewriteMap
directive.RewriteCond
as the last rule has.&s=%{menumap:%2|0}
to the last two rules.