[英]mod_rewrite: Image file name being mistaken as rewrite value
In my .htaccess
file I have this rewrite rule: 在我的
.htaccess
文件中,我有以下重写规则:
RewriteRule example actual-page.php [L]
Which simply rewrites: 只需重写:
www.mysite.com/example
to www.mysite.com/actual-page.php
www.mysite.com/example
到www.mysite.com/actual-page.php
The issue I'm having causes images with example
in the file name (my_example.png), to not load due to the confusion with the rule. 由于与规则混淆,我遇到的问题导致文件名为(my_example.png)的
example
图像无法加载。
I've fixed my problem by changing the rule to: 我通过将规则更改为解决了我的问题:
RewriteRule /example /actual-page.php [L]
I just want to know what the correct solution to this would be. 我只想知道正确的解决方案是什么。 I still have a lot to learn in the world of mod_rewrite, and I want to know if there's some implemented fix to this kind of issue, or if you really are just supposed to make the rule more specific.
在mod_rewrite的世界中,我还有很多要学习的东西,我想知道是否有针对此类问题的一些已实现的修复程序,或者您是否真的应该使规则更具体。
Prepend your rule with this line: 在这一行之前添加规则:
RewriteCond %{REQUEST_FILENAME} !-f
It means that the following rule will only match if the requested url isn't a real file. 这意味着以下规则仅在所请求的URL不是真实文件时才匹配。
Your rewrite rule RewriteRule example actual-page.php [L]
means: 您的重写规则
RewriteRule example actual-page.php [L]
表示:
Find the regular expression example
in the current url and if found, replace the URL with actual-page.php
. 在当前网址中找到正则表达式
example
,如果找到,则将URL替换为actual-page.php
。 And then terminate the rewrite process and ignore further rules (L-flag). 然后终止重写过程,并忽略其他规则(L标志)。
This regular expression will trigger if the string "example" occurs anywhere in the url, including "example.png", but also "another-example-from-a-different-url". 如果字符串“ example”出现在URL中的任何位置(包括“ example.png”,也包括“另一个示例来自另一个URL”),则此正则表达式将触发。
So it is a very good idea to make sure you do not search anywhere with your regular expression, but to tell it to match the entire url string, or at least a significant part. 因此,确保您不要在正则表达式中搜索任何地方,而是告诉它与整个url字符串或至少重要部分匹配,是一个非常好的主意。 The syntactic characters to do so are "^" for "string start" and "$" for "string end".
这样做的语法字符是“ ^”代表“字符串开头”,“ $”代表“字符串结尾”。 This would change your rule to
RewriteRule ^example$ actual-page.php [L]
. 这会将您的规则更改为
RewriteRule ^example$ actual-page.php [L]
。
On the other hand this might now not work, because the url really contains a slash, which is not allowed to match anymore. 另一方面,这可能现在不起作用,因为url确实包含一个斜杠,不再允许该斜杠匹配。 You might add it:
RewriteRule ^/example$ actual-page.php [L]
. 您可以添加它:
RewriteRule ^/example$ actual-page.php [L]
。 Note that the query string is never used in this match, you cannot detect it in the RewriteRule, but would have to use a RewriteCond clause in front of it. 请注意,此匹配项从不使用查询字符串,您无法在RewriteRule中检测到它,但必须在其前面使用RewriteCond子句。
The generic use case for rewriting any virtual url into an existing PHP script, but not touch any existing resources like HTML files, images etc. would be like this: 用于将任何虚拟url重写到现有PHP脚本中但不接触任何现有资源(如HTML文件,图像等)的通用用例如下:
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule .* - [L]
RewriteRule .* php-script.php [L]
This has an inverse logic: If the first rule matches, and the requested filename is either a file, or a directory or a symbolic link, then the rewriting does not take place. 这具有相反的逻辑:如果第一个规则匹配,并且所请求的文件名是文件,目录或符号链接,则不会进行重写。 This will cancel rewriting if a real ressource is requested.
如果请求真正的资源,这将取消重写。 Otherwise the same URL is tried in the second RewriteRule, and the php-script.php is executed and can then analyze the requested URL and act upon it.
否则,在第二个RewriteRule中尝试相同的URL,然后执行php-script.php,然后可以分析所请求的URL并对其执行操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.