Apache URL 重写为domain.com/custom_url_name

发布于 2024-09-24 09:20:26 字数 491 浏览 2 评论 0原文

在 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 技术交流群。

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

发布评论

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

评论(2

尸血腥色 2024-10-01 09:20:26

如果您将斜杠设置为可选会怎样?此外,您可能需要指定比 (.*) 更具体的内容,因为 domain.com/a/b/c/d/e 将匹配。相反,您可以使用否定的 字符类 来指定斜杠以外的所有内容。

RewriteRule ^([^/]*)/?$ store.php?url=$1

或者,如果您只想捕获数字,则可以使用 \d 简写类(匹配任何数字)以及 + 指定至少必须存在一位数字:

RewriteRule ^(\d+)/?$ store.php?url=$1

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.

RewriteRule ^([^/]*)/?$ store.php?url=$1

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:

RewriteRule ^(\d+)/?$ store.php?url=$1
一身仙ぐ女味 2024-10-01 09:20:26

您尝试使用 ^(.*)$ 会失败,因为它会匹配任何 URL 路径。使用比 .* 更具体的模式,也许 \d+ 只允许一个或多个数字:

RewriteRule ^(\d+)$ store.php?url=$1

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:

RewriteRule ^(\d+)$ store.php?url=$1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文