【发布时间】:2013-01-22 06:29:34
【问题描述】:
我有一行代码使用 mod_rewrite 从数据库中提取结果:
example.com/anytext/itemid 使用以下 URL:example.com/item.php?id=itemid
一个简单的重写。但是,我想制定一个优于它的规则。跟着来看看我的意思:
.htaccess 文件示例:
RewriteRule ^products/[^/]+/(.*)$ products/item.php?id=$1 [L]
此规则告诉example.com/products/anytext/itemid 使用item.php 在“/”之后使用 URL 的最后一部分显示,即项目 ID。
但是,我希望重定向某些 URL 而不是重写。例如:
products/green-dress/4023 变成:
products/green-dress-with-stain/4023
没有重写规则!
现在,我在 .htaccess 中尝试了一个简单的 301 重定向,但它以某种方式干扰了重写规则。
而不是去
products/green-dress-with-stain/4023 它转到:
products/green-dress-with-stain4023?id=4023
看看最后的 URL 是如何得到一个变量的?奇怪!
谢谢, 戴夫
【问题讨论】:
-
Mod_alias(例如
Redirect和RedirectMatch指令)优先于 mod_rewrite。您可以使用它们来确保它们首先在 .htaccess 文件中被解析和执行。文件中的位置无关紧要。 -
当我执行“redirect 301 /green-dress/4052 /green-dress-with-stain/4052”时,网址变为:example.com/green-dress-with-stain/4052?id =4052" ...我不明白为什么它在最后添加了ID变量...奇怪!
-
@faa 我猜是因为重写规则干扰了重定向规则......
-
当然。它是下一个,但第一个始终是
Redirect指令。除非你给出一些例子和整个过程的描述,否则很难猜出你想要什么。也许你想用这些信息更新你的问题.. -
@faa 现在说得更清楚了,我希望...
标签: apache .htaccess mod-rewrite