.htaccess 重写规则帮助

发布于 2024-09-26 09:20:41 字数 305 浏览 0 评论 0原文

我的 index.html 中有一个链接:

  href="my_page.php?cat=14&p=1";

现在,我怎样才能重写它,使其看起来像这样:

 /my_page/14/1

到目前为止我已经有了这个,但我不知道如何将结尾“/1”添加到此:

  RewriteRule ^kategori/([0-9_]+)$ browse_cat.php?cat_gr=$1 [NC]

谢谢

I have a link in my index.html:

  href="my_page.php?cat=14&p=1";

Now, how can I rewrite this so that it looks like this:

 /my_page/14/1

I have this so far, but I don't know how to add the ending '/1' to this:

  RewriteRule ^kategori/([0-9_]+)$ browse_cat.php?cat_gr=$1 [NC]

Thanks

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

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

发布评论

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

评论(3

叹梦 2024-10-03 09:20:41

家里没有安装 apache,但这应该可以解决问题。

  RewriteRule ^kategori/([0-9_]+)(?:/(0-9]+))?$ browse_cat.php?cat_gr=$1&p=$2 [NC]

(?:...)?使第二部分可选。所以 /kategori/14 和 /kategori/14/1 应该可以工作。

如果没有,你需要两条规则

  RewriteRule ^kategori/([0-9_]+)$ browse_cat.php?cat_gr=$1 [NC]
  RewriteRule ^kategori/([0-9_]+)(?:/(0-9]+))?$ browse_cat.php?cat_gr=$1&p=$2 [NC]

Have no apache installed here at home, but this should do the trick.

  RewriteRule ^kategori/([0-9_]+)(?:/(0-9]+))?$ browse_cat.php?cat_gr=$1&p=$2 [NC]

(?: ...)? makes the second part optional. So /kategori/14 as well as /kategori/14/1 should work.

If not, you'll need two rules

  RewriteRule ^kategori/([0-9_]+)$ browse_cat.php?cat_gr=$1 [NC]
  RewriteRule ^kategori/([0-9_]+)(?:/(0-9]+))?$ browse_cat.php?cat_gr=$1&p=$2 [NC]
最初的梦 2024-10-03 09:20:41
ReWriteCond {%REQUEST_URI} ^/my_page/([0-9]*)/([0-9]*)
ReWriteRule .* /my_page.php?cat=%1&p=%2 [NC,L]

仅当至少指定了 cat 和 p 时,这才有效。

ReWriteCond {%REQUEST_URI} ^/my_page/([0-9]*)/([0-9]*)
ReWriteRule .* /my_page.php?cat=%1&p=%2 [NC,L]

This would only work if both cat and p at a minimum are specified.

再见回来 2024-10-03 09:20:41

如果您实际上使用的是 /my_page.php?cat=14&p=1 之类的 URL,并希望它们“显示”为 /my_page/14/1,您将需要两条规则:一条将 /my_page.php?cat=14&p=1 从外部重定向到 /my_page/14/1,一条重写 /my_page/14/1 内部到 /my_page.php?cat=14&p=1

RewriteCond %{THE_REQUEST} ^GET\ /my_page\.php\?
RewriteCond %{QUERY_STRING} ^cat=([0-9]+)&p=([0-9]+)$
RewriteRule ^my_page\.php$ /my_page/%1/%2 [L,R=301]

RewriteRule ^my_page/([0-9]+)/([0-9]+)$ my_page.php?cat=$1&p=$2 [L]

请注意,此规则仅适用于该特定 URL(数字和顺序或参数)。更通用的规则会变得更复杂;在这种情况下,使用 PHP 等高级语言可能更容易。

此外,如果您已经在 HTML 文档中使用“正确”的 URL,而不是将每个请求重定向/重写两次,那就更好了。

If you’re actually using URLs like /my_page.php?cat=14&p=1 and want them to be “displayed” as /my_page/14/1, you will need two rules: One to redirect /my_page.php?cat=14&p=1 externally to /my_page/14/1 and one to rewrite /my_page/14/1 internally to /my_page.php?cat=14&p=1.

RewriteCond %{THE_REQUEST} ^GET\ /my_page\.php\?
RewriteCond %{QUERY_STRING} ^cat=([0-9]+)&p=([0-9]+)$
RewriteRule ^my_page\.php$ /my_page/%1/%2 [L,R=301]

RewriteRule ^my_page/([0-9]+)/([0-9]+)$ my_page.php?cat=$1&p=$2 [L]

Note that this rule does only work with this specific URL (number and order or parameters). A more general rule will get more complex; using a higher level language like PHP is probably easier in this case.

Furthermore, it would be better if you already use the “right” URL in your HTML documents rather than redirecting/rewriting every request twice.

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