Apache URL 重写为domain.com/custom_url_name
在 Red Hat 服务器上使用 Apache,我尝试将我们网站上会员商店的 URL 从: 重写
domain.com/store.php?url=12345
为:
domain.com/12345
使用这些规则,如果我始终记得添加结尾斜杠,我就可以让它工作:
Options -Indexes
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^store/url/(.*)$ store.php?url=$1
RewriteRule ^(.*)/$ store.php?url=$1
domain.com /12345/作品, 但domain.com/12345 不起作用。
删除 Rewrite 代码最后一行中的斜杠会破坏很多东西。有没有办法让它在有或没有结尾斜杠的情况下都起作用?
Using Apache on a Red Hat server, I'm trying to rewrite the URL of a member's store on our website from:
domain.com/store.php?url=12345
to:
domain.com/12345
Using these rules, I can get it to work if I always remember to add an ending slash:
Options -Indexes
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^store/url/(.*)$ store.php?url=$1
RewriteRule ^(.*)/$ store.php?url=$1
domain.com/12345/ works,
but domain.com/12345 does not work.
Removing the slash in the last line of Rewrite code breaks a lot of stuff. Is there a way to get this to work both with or without that ending slash?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您将斜杠设置为可选会怎样?此外,您可能需要指定比
(.*)
更具体的内容,因为 domain.com/a/b/c/d/e 将匹配。相反,您可以使用否定的 字符类 来指定斜杠以外的所有内容。或者,如果您只想捕获数字,则可以使用
\d
简写类(匹配任何数字)以及+
指定至少必须存在一位数字:What if you made the slash optional? Furthermore, you probably to to specify something more specific than
(.*)
, because domain.com/a/b/c/d/e will match. Instead, you can use a negated character class to specify everything other than a slash.Alternately, if you only want to capture numbers, you can use the
\d
shorthand class (which matches any digit) along with a+
which specifies that at least one digit must be present:您尝试使用
^(.*)$
会失败,因为它会匹配任何 URL 路径。使用比.*
更具体的模式,也许\d+
只允许一个或多个数字:Your attempt using
^(.*)$
fails because that would match any URL path. Use a more specific pattern than.*
, maybe\d+
to allow only one or more digits: