我的网址:
http://www.mywebsite.com/i/1Q2GmFuFdZ.jpg
我想重写规则,使得它去到文件 image.php?ID = 1Q2GmFuFdZ.jpg
I want to rewrite a rule that makes it go to the file image.php?id=1Q2GmFuFdZ.jpg
在我的PHP文件,我有这样的
In my PHP file, I have this
<?PHP
print_r($_GET);
?>
这是我在我的htaccess
This is what I have in my htaccess
RewriteEngine On
RewriteRule ([a-zA-Z0-9]+[.]+[a-z]{3})$ image.php?id=$1
这是我得到的输出:
Array ( [id] => image.php )
我不知道我做错了。我不是正则表达式亲,但我知道的基本知识。
I am not sure what I am doing wrong. I'm no regex pro but I know the basics.
感谢
编辑: 忘了补充0-9在EX pression
Forgot to add 0-9 in expression
mod_rewrite规则的递归应用。下面是当您请求 1Q2GmFuFdZ.jpg
会发生什么:
mod_rewrite rules are applied recursively. Here is what happens when you request 1Q2GmFuFdZ.jpg
:
1Q2GmFuFdZ.jpg
匹配模式,变为 image.php?ID = 1Q2GmFuFdZ.jpg
由于文件名已经改变,另一次迭代与所述重新编写的URL执行
image.php
的匹配模式以及的并改写为 image.php ?ID = image.php
在文件名或目录并没有变化,所以没有进一步的迭代执行
最终结果: image.php ID = image.php
1Q2GmFuFdZ.jpg
matches the pattern, becomes image.php?id=1Q2GmFuFdZ.jpg
Since filename has changed, another iteration is performed with the re-written URL
image.php
matches the pattern as well and rewritten as image.php?id=image.php
File name or directory did not change so no further iteration is performed
End result: image.php?id=image.php
解决方法:添加此规则:
Solution: add this rule:
RewriteEngine On
RewriteRule image.php$ - [L]
RewriteRule ([a-zA-Z0-9]+.[a-z]{3})$ image.php?id=$1
第二条规则作为手制动。你需要调整规则#3为好。
The second rule acts as a hand-brake. You need to tweak rule #3 as well.