简体   繁体   中英

.HTACCESS and Redirecting

I've got this .htaccess file which redirects .PNG's to .PHP files (for dynamic image), however, I don't want to do that with all the .PNG's in the folder, as when I want to place a normal static image on my page, I can't, as it will will create a dynamic image.

RewriteEngine On
RewriteRule ([a-zA-Z\0-9\s]+)\.png image.php?name=$1

I'm not very experienced with .HTACCESS, so any help would be appreciated, thank you.

You can add a RewriteCond which skips the following RewriteRule if a file !-f or directory !-d exists. I've included both for your reference - you might only want to use the !-f one.

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ([a-zA-Z\0-9\s]+)\.png image.php?name=$1 [L]

An example where this would be useful is if you have a php script which creates thumbnail images. Point your html page at where the thumbnail .png should exist, and when it doesn't, redirect to a .php page which creates it, then both writes it to disk and serves it directly. The next request will hit the RewriteCond and find a matching thumbnail and serve it directly, bypassing the rewrite.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM