How to redirect all requests to a file, except images using Apache?

拟墨画扇 提交于 2019-11-29 01:16:36
undone

As I understand you want to process all non-images threw your php file. Right?

If so, then here is what you need:

Options +FollowSymLinks +ExecCGI

<IfModule mod_rewrite.c>

RewriteEngine On
RewriteCond %{REQUEST_URI}  !(\.png|\.jpg|\.gif|\.jpeg|\.bmp)$
RewriteRule (.*)  entryPoint.php [QSA]

</IfModule>

Remember to add to delete any image extension you want! If I misunderstood,Please tell me to correct my answer!

You can use something like this to stop rewriting images

RewriteRule \.(jpg|png)$ - [L]

This rule says 'if the requested file ends with a period followed by either "jpg" or "png" then don't rewrite it.'

The $ character just marks "the end of the string/filename", the - says "don't rewrite the URL if this rule matches", and the [L] means "don't process any more rules if this one matches.

I'll add that the usual way of dealing with this sort of thing is to just apply a blanket statement of "if this file exists just serve the file up directly, but anything that doesn't exist gets processed by PHP." That type of rule looks like this:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?request=$1 [L,QSA]

This says "if the request isn't for a file or directory that exists, pass the request to index.php with the original path passed as a query string parameter called 'request', appending any other query string parameters to the new request."

RewriteCond %{REQUEST_FILENAME} !\.(png|jpg|gif)$

Put this line right before your RewriteRule

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!