好的,所以我正在为自定义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
有帮助吗?

解决方案 2

我以不同的方式解决了这一点:对store.php文件进行修改。

我看了从 print_r($_SERVER) 在播种正常和重写的URL之后。我找到 $_SERVER['SCRIPT_URL'] 当打击正常URL时,包含“/store.php”,并且在重写URL时包含我的重写路径。

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

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");
}

其他提示

重定向仅使用URL前缀,而不需要纠正措施(例如 /store.php或 /store)

您需要尝试redirectMatch:

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

另外,它应该以A /开头?我不确定(例如,您的重写条目以无斜线开头)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top