htaccess: Can't check dynamic file existence!

China☆狼群 提交于 2019-12-11 12:16:05

问题


I have written a on-the-fly thumbnail creator by an htaccess file looking to see if the requested file exists and if not sending the user to a php page to create it. My htaccess file looks like this:

# Check the formatting (this works)
RewriteCond %{SCRIPT_FILENAME}%{QUERY_STRING} /([a-zA-Z0-9-]+).(jpg|gif|png)w=([0-9]+)&h=([0-9]+)(&c=(true|false))

# Only forward to thumbnail.php if file doesn't exist
#RewriteCond %{REQUEST_FILENAME} !-f   #this works but not for the correct filename!
RewriteCond %1-%2-%3-%4-%6.jpg !-f     # Doesn't work!!!

# If file doesn't exist, send here: (this works)
RewriteRule ^.*$ thumbnail.php?file_name=%1&type=%2&w=%3&h=%4&c=%6

My check of the file's existence does not seem to work however... the old version that uses %{REQUEST_FILENAME} works, but that is not the correct filename I need to check. I have verified that the new file %1-%2-%3-%4-%6.jpg has the correct output, but does not trigger the condition!

Based on this URL:

this-is-the-image-name.gif?w=200&h=100&c=true

The htaccess file should check if this file exists:

this-is-the-image-name-gif-200-100-true.jpg

The thumbnails and htaccess file are both in the same folder, but this folder is unknown and can't be hardcoded. Does it need the full path to check? If so, how can I get the full path but with my custom filename?

Pulling my hair out with this one... PLEASE help! Thank you.


回答1:


You can use your PHP script to replace the traditional "404 Error" page :

ErrorDocument 404 thumbnail.php

Then, you can retrieve the requested URL using PHP :

$url = $_SERVER["REQUEST_URI"];

Parameters can be retrieved using the preg_match function.



来源:https://stackoverflow.com/questions/6302529/htaccess-cant-check-dynamic-file-existence

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