htaccess 组合规则
我有 2 个 htaccess 重写规则:
RewriteRule ^tag/([^/\.]+)/?$ index.php?p=tag&tag=$1&pg=1 [L]
RewriteRule ^tag/([^/\.]+)/([0-9]+)/?$ index.php?p=tag&tag=$1&pg=$2 [L]
示例:
1- mysite.ext/tag/php/
2- mysite.ext/tag/php/13/
是否可以组合2,知道第二个参数(页码)是可选的,因为它用于分页?
因此“pg”将是 (1|$2)
(pg=1 或 pg=the-page-number)
I have 2 htaccess rewrite rules:
RewriteRule ^tag/([^/\.]+)/?$ index.php?p=tag&tag=$1&pg=1 [L]
RewriteRule ^tag/([^/\.]+)/([0-9]+)/?$ index.php?p=tag&tag=$1&pg=$2 [L]
example:
1- mysite.ext/tag/php/
2- mysite.ext/tag/php/13/
Is it possible to combine the 2, knowing that the second argument which is the page number is optional as it is for pagination?
So the "pg" will be (1|$2)
(pg=1 or pg=the-page-number)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
“因此“pg”将是 (1|$2)(pg=1 或 pg=页码)”
不幸的是,它不是这样工作的。您能做的最好的事情就是使用此规则:
如果请求
mysite.ext/tag/php/
,那么它将被重写为index.php?p=tag&tag=php& pg=。
pg
参数为空(如您所见)。您需要做的就是在index.php
中添加额外的代码来检查pg
是否为空,然后分配1
的值:上面的代码可能看起来太多(太安全的方法),但这就是我更喜欢做的(在做电子商务网站时,安全是第一要务)。 (我已经跳过了完整的验证以使其更短)
但是如果您愿意,您可以将其变得更小(无论如何您仍然必须验证输入数据):
"So the "pg" will be (1|$2) (pg=1 or pg=the-page-number)"
Unfortunately it does not work like that. The best what you can do is to use this rule:
If
mysite.ext/tag/php/
is requested then it gets rewritten toindex.php?p=tag&tag=php&pg=
. Thepg
parameter is empty (as you can see). What you need to do -- is to put extra bit of code inindex.php
to check ifpg
is empty then assign value of1
:The code above may look as too much (too safe approach), but that's what I prefer to do (when doing e-commerce sites security is first priority). (I've skipped the full validation to make it shorter)
But you can make it much smaller, if you wish (you still have to validate the input data anyway):