I would like when accessing the url https://www.italinea.com.br/uploads/jx5rufam7adfwd75pi6c.jpg, which is an existing file on the server, open the php file https://www.italinea.com.br/image.php.
Which htaccess rule do I use?
I tried to use:
RewriteRule ^(.+)\.jpg image.php [L,QSA]
But as it is an existing file, it opens the image and not the .php file
RewriteRule ^(.+)\.jpg image.php [L,QSA]
This is the right idea and should "work", although it could be simplified a bit. And it is rather generic, so matches every .jpg request.
If this is not working then either:
You have a conflict with other directives in the .htaccess file (the order of directives can be important).
You have a front-end proxy that is serving your static content. This is a problem if the URL being requested maps to a physical file as the application server (ie. .htaccess) is then completely bypassed.
If this is the case then see my answer to the following related question:
WordPress: can't achieve direct image access redirection via .htaccess
Background:
I've recently created a dynamic image resizing script in PHP so that I can give it an image path somewhere within the server + get parameter of width, and it'll return this image, resized according to the query parameter and throw the image into a cache folder for future use.
Afterwards, if the same image, with the same width is requested, it goes to that PHP script, and the script checks if the file already exists, and if it does, it'll just output it. (It will do file_get_contents(), and then echo it with the appropriate header)
The challenge:
What I want to do is to bypass the PHP script if the file already exists. I want .htaccess to check if the file exists in the cache folder, and then simply go to that file, instead of the PHP script. That would be simple enough, except the filename includes the modified width.
So, for instance:
If the file relative path is images/products/36sh542dhs.jpg, and I want it modified with width 100px, the request URL will look like this si/images/products/36sh542dhs.jpg?d=100. The file will be stored as follows: resized_images/cache/images/products/36sh542dhs---D100.jpg.
Is there any conceivable way to get .htaccess to take in si/images/products/36sh542dhs.jpg?d=100, break it up, remove si, add resized_images/cache instead, AND modify the filename to stick the D---100 right before the "dot extension" part?
The best algorithm that comes to my mind regarding your issue is to: always use the /resized_images/cache/images/products/36sh542dhs---D100.jpg path.
In .htaccess you can have a line ErrorDocument 404 /404-error-handler.php. This will catch all 404 errors.
In 404-error-handler.php file you check the value of $_SERVER['REQUEST_URI']. If it contains your path, you include the logic of image generation and saving else you return a custom 404 error page.
This way you will obtain what you have asked: htaccess check if file exists, if no it goes up the 404 custom php script which will check if the URL matches your image-generation-url and either generate and cache the image, either show a 404 page. If the file exists, it will be served to client.
Another way (kind of the way you were suggesting)...
Link to:
/si/images/products/36sh542dhs---D100.jpg
Cached images stored in:
/resized_images/cache/images/products/36sh542dhs---D100.jpg
Script that actually resizes images:
/scripts/image-resizer.php?image=36sh542dhs&size=100
By linking to a different URL you can keep your cache directory and image script "private" - which can be easily moved to different locations if you wish.
Using mod_rewrite in .htaccess:
RewriteEngine On
# Serve cached image if it exists
RewriteCond %{DOCUMENT_ROOT}/resized_images/cache/$1 -f
RewriteRule ^si/(images/products/\w---D\d{2,4}\.jpg)$ resized_images/cache/$1 [L]
# Otherwise send request to PHP image resizing script
RewriteRule ^si/images/products/(\w)---D(\d{2,4})\.jpg$ scripts/image-resizer.php?image=$1&size=$2 [L]
Alternatively, you link directly to the cached image (as mentioned in #besciualex's answer) and rewrite the request to your image resizer script when it doesn't exist, rather than relying on the error handler:
# Send request to PHP image resizing script when cached image does not exist
RewriteCond %{REQIEST_FILENAME} !-f
RewriteRule ^resized_images/cache/images/products/(\w)---D(\d{2,4})\.jpg$ scripts/image-resizer.php?image=$1&size=$2 [L]
Only requests that look-like cached images are checked.
I have a record shop online.
The following URL example shows the product info page without a problem:
http://www.example.org/tp/html/product/33/record1.html
Im using an .htaccess file as follows:
RewriteEngine On
RewriteRule ^product/([0-9]+)/(.*).html$ detail.php?id=$1 [L]
My problem is that the sound files are in a different folder on the server to the detail.php file.
The mp3 sound files are not shown on the page as they are in a different folder.
How do i tell the .htaccess file to look in the ../../sf folder to find the mp3 files?
thanks!
I have a site https://sitename.com/web (example). So, full path to page is sitename.com/web/index.php. On ftp server there is folder /web which also contain second page file - index-2.php. How to avoid creating /web-2/ folder and redirect index-2.php to sitename.com/web-2 ?
Easy, create an .htaccess file inside the root folder of your site, then add the following lines to it:
RewriteEngine On
RewriteRule ^web-2$ index-2.php [QSA,L,NC]
This site might help you with htaccess:
https://www.htaccessredirect.net/
A nice tutorial on .htaccess files also below:
https://www.digitalocean.com/community/tutorials/how-to-use-the-htaccess-file
I am looking for a away to redirect a visitor trying to access my download folder
http://domain.com/downloads/
- this is the download folder where my files are located.
I want to redirect the visitor to a registration page.
and also if they try to direct download the file
http://domain.com/downloads/installer.exe
I would like to get redirected to my registration page
http://domain.com/registration/register.html
I think this will do it.
RewriteRule ^downloads(.*)$ http://domain.com/registration/register.html [R=301,L]