重定向到重写的 URL

发布于 2024-12-18 05:30:52 字数 538 浏览 3 评论 0原文

好的,我正在为自定义 PHP 购物车重写一些页面 URL。

我有以下规则:

RewriteRule ^category/([0-9]+)/([a-z-]+)$ /store.php?cat=$1 [L]
RewriteRule ^product/([0-9]+)/([a-z-]+)$ /product.php?id=$1 [L]

这些规则允许我使用像 example.com/product/23/product-slug 这样的 url 结构。

那部分工作正常。我想知道我对另一个方向有什么选择;将旧 url 的请求重定向到新 url。例如,当有人访问 /product.php?id=2 时,我想重定向到 /products/2/slug。

知道如何完成这件事吗?

我尝试了一个简单的重定向,但不起作用:

Redirect 301 ^/store\.php\?cat=16$ http://www.example.com/category/16/category-slug

OK, so I'm rewriting some page URLs for a custom PHP cart.

I've got the following rules:

RewriteRule ^category/([0-9]+)/([a-z-]+)$ /store.php?cat=$1 [L]
RewriteRule ^product/([0-9]+)/([a-z-]+)$ /product.php?id=$1 [L]

These will allow me to use a url structure like example.com/product/23/product-slug.

That part is working alright. I'm wondering what options I have for the other direction; redirecting requests for the OLD url to the NEW url. So for example, when someone goes to /product.php?id=2 I want to redirect to /products/2/slug.

Any idea how to get this done?

I tried a simple redirect, but would not work:

Redirect 301 ^/store\.php\?cat=16$ http://www.example.com/category/16/category-slug

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

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

发布评论

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

评论(2

寂寞花火° 2024-12-25 05:30:52

重定向仅需要 url 前缀,而不是正则表达式(例如 /store.php 或 /store)

您需要尝试 RedirectMatch:

RedirectMatch 301 ^/store\.php\?cat=16$ http://www.example.com/category/16/category-slug

另外,它是否应该以 / 开头?我不确定(例如,上面的 RewriteRule 条目不以斜线开头)

Redirect only takes a url prefix, not a regex (e.g. /store.php or /store)

You need to try RedirectMatch:

RedirectMatch 301 ^/store\.php\?cat=16$ http://www.example.com/category/16/category-slug

Also, is it supposed to start with a /? I'm not sure (your RewriteRule entries above start with no slash, for example)

寂寞笑我太脆弱 2024-12-25 05:30:52

我用不同的方式解决了这个问题:修改 store.php 文件。

在 ping 正常的 URL 和重写的 URL 后,我查看了 print_r($_SERVER) 的输出。我发现当正常网址被点击时, $_SERVER['SCRIPT_URL'] 包含“/store.php”,而当点击重写网址时,它包含我重写的路径。

这意味着我可以做一个简单的测试并适当地重定向:

if ($_SERVER['SCRIPT_URL'] == "/store.php") {
    // run some code that will generate the url
    $rewrittenURL = generateURL();
    // then add:
    header("HTTP/1.0 301 Moved Permanently"); // tell spiders this is permanent
    header("Location: $rewrittenURL");
}

I solved this a different way: with a modification to the store.php file.

I looked at output from print_r($_SERVER) after pinging both the normal and rewritten urls. I found that $_SERVER['SCRIPT_URL'] contains "/store.php" when the normal url is hit and it contains my rewritten path when the rewritten url is hit.

This means I can do a simple test and redirect appropriately:

if ($_SERVER['SCRIPT_URL'] == "/store.php") {
    // run some code that will generate the url
    $rewrittenURL = generateURL();
    // then add:
    header("HTTP/1.0 301 Moved Permanently"); // tell spiders this is permanent
    header("Location: $rewrittenURL");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文