Php 重定向到重写的URL

Php 重定向到重写的URL,php,mod-rewrite,mod-alias,Php,Mod Rewrite,Mod Alias,好的,我正在为一个定制的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的请求重

好的,我正在为一个定制的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

重定向只采用url前缀,而不采用正则表达式,例如/store.php或/store

您需要尝试重定向匹配:

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

还有,它应该以/开头吗?我不确定上面的RewriteRule条目是否以无斜杠开头,例如,我用另一种方法解决了这个问题:修改store.php文件

在ping了正常和重写的URL之后,我查看了print_r$\服务器的输出。我发现$\u SERVER['SCRIPT\u 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");
}

仅供参考-重定向是mod_别名的一部分,而不是mod_rewrite.RedirectMatch 301^/store\.php\?cat=16$不起作用。