php中的url重写

发布于 2024-09-02 07:10:52 字数 185 浏览 0 评论 0原文

我对apache mod_rewrite还有一个疑问。 我想重写网址 mydomain/index.php?category=1&id=1 到 mydomain/索引/类别/1/id/1 我如何在 .htaccess 中编写规则

我必须在 a 标记内给出的链接是什么

请给我一个解决方案..

Hai

I have one more doubt in apache mod_rewrite.
I want to rewrite the url
mydomain/index.php?category=1&id=1
To
mydomain/index/category/1/id/1
How I write rule in .htaccess

And what is the link that i have to give inside the a tag

Please give me a solution..

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

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

发布评论

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

评论(2

小兔几 2024-09-09 07:10:52

未经测试,但值得一试:

RewriteEngine On
RewriteRule ^index/category/([0-9]+)/id/([0-9]+)$ index.php?category=$1&id=$2

您的网址可以看起来与您提到的方式完全相同:

Category 1
<a href="index/category/1/id/1">Product 1</a>
<a href="index/category/1/id/2">Product 2</a>
Category 2
<a href="index/category/2/id/3">Product 3</a>
<a href="index/category/2/id/4">Product 4</a>

Not tested, but worth a shot:

RewriteEngine On
RewriteRule ^index/category/([0-9]+)/id/([0-9]+)$ index.php?category=$1&id=$2

Your URLs can look exactly like the way you mentioned:

Category 1
<a href="index/category/1/id/1">Product 1</a>
<a href="index/category/1/id/2">Product 2</a>
Category 2
<a href="index/category/2/id/3">Product 3</a>
<a href="index/category/2/id/4">Product 4</a>
乖不如嘢 2024-09-09 07:10:52

标签内,您将使用很好的链接,即 category/1/id/1 (这正是您使用 mod_rewrite< /code>,以便能够使用漂亮的 URL!)

至于规则,请尝试类似(未经测试)的内容:

RewriteRule category/(.*)/id/(.*)$ index.php?category=$1&id=$2&%{QUERY_STRING} [L]

实际上我宁愿使用

RewriteRule (.*)/(.*)$ index.php?category=$1&id=$2&%{QUERY_STRING} [L]

So you can direct call mydomain/1/1 但你得到了我希望编辑的想法

&%{QUERY_STRING}部分对于您的要求来说不是必需的,但我通常会包含它,以防我想传递任何额外的参数到页面。

Inside the <a> tags you will use the nice link, that is category/1/id/1 (that's exactly why you're using mod_rewrite, to be able to use nice URLs!)

As for the rule, try something like (untested):

RewriteRule category/(.*)/id/(.*)$ index.php?category=$1&id=$2&%{QUERY_STRING} [L]

Actually I would rather use

RewriteRule (.*)/(.*)$ index.php?category=$1&id=$2&%{QUERY_STRING} [L]

So you can call directly mydomain/1/1 but you got the idea I hope

EDIT: the &%{QUERY_STRING} part is not necessary for what you asked, but I generally include it in case I want to pass any extra parameter to the page.

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