重定向到一个重写的URL
好吧,所以我重写了一些自定义PHP购物车的页面URL。重定向到一个重写的URL
我有以下规则:
RewriteRule ^category/([0-9]+)/([a-z-]+)$ /store.php?cat=$1 [L]
RewriteRule ^product/([0-9]+)/([a-z-]+)$ /product.php?id=$1 [L]
这将允许我使用一个url结构像example.com/product/23/product-slug。
这部分工作正常。我想知道我对于另一个方向有什么选择;将旧网址的请求重定向到新网址。例如,当有人前往/product.php?id=2时,我想重定向到/ products/2/slug。
任何想法如何做到这一点?
我试过一个简单的重定向,但不会工作:
Redirect 301 ^/store\.php\?cat=16$ http://www.example.com/category/16/category-slug
我解决了这个问题:修改了store.php文件。
在Ping了正常URL和重写URL之后,我查看了print_r($_SERVER)
的输出。我发现$_SERVER['SCRIPT_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");
}
重定向只需要一个网址的前缀,而不是一个正则表达式(如/store.php或/店)
你需要尝试RedirectMatch:
RedirectMatch 301 ^/store\.php\?cat=16$ http://www.example.com/category/16/category-slug
此外,它应该以一个/?开头,我不确定(例如,上面的RewriteRule条目以无斜线开头)
RedirectMatch 301 ^/store \ .php \?cat = 16 $ http://www.exmaple.com/category/16/cat-slug无法正常工作。 – Jazzerus
仅供参考 - 'Redirect'是'mod_alias',不'mod_rewrite'的一部分。 – Kato