在htaccess中使用重定向301或rewriterule重定向并随身携带一个php变量
I think my question is quite simple but I've been banging my head against the wall for the past few hours.
I have my website using rewriterule to ensure messy path names with php variables are now nice and tidy(I've removed [http://www] from my examples because the system thinks I am putting links and won't let me).
So somebody comes to my site at mysite.co.uk/my-product-P1.html the website will know to post mysite.co.uk/product.php?id=1 to the server.
But I also want to tidy it up the other way around. If an old customer or an old link uses the pathname mysite.co.uk/product.php?id=1 then I want it to return mysite.co.uk/my-product-P1.html instead even though the old pathname will actually still work. I don't want customers accessing the same page from different pathnames.
How do I do this and will it create a loop? On another website I have it working using:
RewriteCond %{QUERY_STRING} ^id=1$ [NC]
RewriteRule ^product\.php$ product-P1.html? [R=301,L]
But on that site there are only around 10 products so I'm able to write these lines for each products. On my other site I have hundreds of products so this isn't practical and I need to do it automatically.
Hopefully this makes sense. I have read through other posts and can't find my solution so apologies if this is clearly explained somewhere else.
我认为我的问题很简单,但过去几个小时我一直在撞墙。 p>
我的网站使用rewriterule来确保使用php变量的凌乱路径名称现在很好而且整洁(我从我的示例中删除了[http:// www]因为系统认为我 我正在放置链接,不会让我这么做。 p>
所以有人来到我的网站mysite.co.uk/my-product-P1.html,该网站将知道发布mysite。 co.uk/product.php?id=1到服务器。 p>
但我也希望以相反的方式整理它。 如果旧客户或旧链接使用路径名mysite.co.uk/product.php?id=1,那么我希望它返回mysite.co.uk/my-product-P1.html,即使旧路径名将 实际上还在工作。 我不希望客户从不同的路径名访问同一页面。 p>
如何执行此操作并创建循环? 在另一个网站上我使用它: p>
RewriteCond%{QUERY_STRING} ^ id = 1 $ [NC]
RewriteRule ^ product \ .php $ product-P1.html? [R = 301,L]
code> pre>
但在该网站上只有大约10种产品,因此我可以为每种产品编写这些产品线。 在我的其他网站上,我有数百种产品,所以这不实用,我需要自动完成。 p>
希望这是有道理的。 我已阅读其他帖子,如果在其他地方清楚地解释了这一点,就无法找到我的解决方案。 p>
div>
How do I do this and will it create a loop?
The rules that you have (on the site with 10 products) need to match against the actual request as opposed to the URI:
RewriteCond %{THE_REQUEST} \ /product\.php\?id=([^\ &]+)
RewriteRule ^ /product-P%1.html? [R=301,L]
But you're better off doing this in your php script rather than enumerating all the products in the htaccess file:
On my other site I have hundreds of products so this isn't practical and I need to do it automatically
You can't do that using only mod_rewrite. You'll need to script that in your product.php
script. The product.php
script will need to check the $_SERVER'[REQUEST_URI']
php variable, and see if it starts with: /product.php
.
If it does, then you know someone accessed the php script directly, and you'll need to fetch the product name using the id passed in $_GET['id']
, then redirect to the product name + "-P$_GET['id'].html"
.
The htaccess file and mod_rewrite won't know the mapping between product IDs and product names, so you need to do this in your php script (which does have access to this mapping).
You need this additional rule:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+product\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /product-P%1.html? [R=302,L]