404 Image Placeholder - .htaccess

I've got a product gallery I'm working on and just did an import. This added a bunch of references to images that don't exist on the server.
Right now when the image tries to load, it's got that broken image icon, which is ugly, and well, should be handled better.
Ideally, whenever a broken image would be displayed -- that is to say when an image's path just doesn't exist -- I would like Apache to show a placeholder image instead.
Is there any way to do this via .htaccess?

Use RewriteCond with -f to check if the file exists, e.g.:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*(\.gif|\.jpe?g|\.png)$ placeholder.jpg [L]

Related

redirect single image from one directory to another

I have one website, say www.example.com. So when I access "http://www.example.com/program/resources/foo.gif" it serves me foo.gif image from /usr/share/roundcube/program/resources/foo.gif, I found that in access log.
So what I would like to do is, I would like to copy that image from /usr/share/roundcube/program/resources/ to my webroot /var/www/www.example.com/webroot/img/ and write rewrite rule so that when request comes for foo.gif, it should serve from /var/www/www.example.com/webroot/img not from /usr/share/roundcube/program/resources/.
I've tried this
RewriteEngine On
RewriteRule /program/resources/(.*) /img/$1 [L]
It's working fine. But what if I want to make a rule for single image that is foo.gif ?
You can make it more generic and simpler than that.
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/override%{REQUEST_URI} -f
RewriteRule .* override/$0 [L,NS]
Now just put any file you don't want to come from RoundCube into the override folder, e.g. override/program/resources/foo.gif

My page is loading several times

My website is loading 3 times every time I go to
website/user/slug.
This is my htaccess file.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
#RewriteCond %{REQUEST_FILENAME} !\.(png|jpg|gif)$
#redirect to index.php?url=slug
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
Anyone can give me a hint on how to solve this multiple loading?
So, This is not a complete answer but I'm updating this until I get the full answer.
As suposed on one of the answers, I'm using my own framework, it works by looking at the url and including the view in index.php as all the components necessary to this page.
So, as said the page was loading 3 times ( or making 3 entries on the database ), and I've stated digging my loading function, on wich I found a script that when a user is not activated it redirects to the home page. This header(location blah) was making the page load one extra time.
I've changed it to include home view and by now the page is only loading twice.
As far as I'm concerned I don't have any other redirect on the php scripts. I'll triple check but if not, I have to update this answer.
UPDATE -> at this point I'm having a strange situation here. I've run all my php and javascript code, and they are clean. I've finnaly discovered the bit of code that is giving me this problem, although it's a strange situation:
It seems that the bootstrap modal is giving me a redirect and I don't now why. I just have the html code at this point so I'm checking for javascript code on it and afterwards I'll look at bootstrap.js in order to find why this is happening.
I'll keep this thread updated !
UPDATE-> OK ! Now I have the full answer, it seems it wasn't bootstrap but an image I had. So I had this image called blahh that it's purpose was to preview an uploaded image, so, since the image didn't have a url defined, the it was redirecting ( the explanation is something like this lol ! I'm not even quite sure of what's happening here but giving the image an url I stopped having this error ).

.htaccess Redirect in case of 404

I have this line of .htaccess
This line is used to get images from another server.
RewriteRule ^resources/fabricantes(.*)$ http://mysecondserver.com/arq/pictures/fab$1
than, if I have the url: http://myserver.com/resources/fabricantes/fab_1.jpg
this image will be get from: http://mysecondserver.com/arq/pictures/fab/fab_1.jpg
The Problem:
In some cases, the image doesn't exists on mysecondserver.com, how can I redirect to a "image unavailable" image in this cases?
First think you need to understand that this rule can only work from mysecondserver.com host not from server.com.
On mysecondserver.com place this .htaccess in /arq/pictures/.htaccess:
RewriteEngine On
RewriteBase /arq/pictures/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ no-image.jpg [L]
On mysecondserver.com, you would have .htaccess checking for -f status of the REQUEST_FILENAME. It could send the user back to the first server, if necessary. The alternative is to explicitly list all known failures back on your first server, which is a lot of work for you (even if you have a complete list). There's no way for the first server to know if a file actually exists on the second server.

Sending path and GET to index.php

Let me give you an example:
I open up this address using my browser: http://localhost/AAA/BBB/CCC?id=5&foo=bar
I want to write a .htaccess that implicitly converts the link to: http://localhost/index.php?route=/AAA/BBB/CCC&id=5&foo=bar
Whatever the destination link will be, I don't care; I just want it to contain both path, and GET request. Notice that some URL like the one below don't contain any parameter, but must be parsed correctly:
http://localhost/AAA/BBB
will be converted to http://localhost/index.php?route=/AAA/BBB
Thanks in advance
You just have to capture whatever request url is present and append it as "route":
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?route=$1 [L,QSA]
A side note: if you have access to the real server configuration, then prefer that to .htaccess style files. Those files make things more complex, are notoriously error prone and make the server sloooooow!

CakePHP: Can you globally modify all links to point to another directory?

My CakePHP app lives inside a subdirectory to keep it from crashing into a Wordpress installation that powers part of the website:
example.com/ <--root
/_wp <--Wordpress installed here
/page1
/page2
/_cake <--CakePHP installed here
/page3
/page4
To maintain consistency, I'm using mod_rewrite rules to rewrite URLs from example.com/_cake/pageX to example.com/pageX etc. This basically works, but CakePHP still creates links with /_cake/pageX. So if a user hovers over a link, the "_cake" will show up in the bottom of the browser and of course in the source code.
Is there a way to configure CakePHP to think it's actually in the site root so it creates the desired URLs for links etc?
I haven't found a way to configure CakePHP the way you want.
If you create your links with HtmlHelper::link or Router::url, you can add a member $url['base'] = false to the $url array argument. This prevents the _cake prefix being inserted in front of the URL.
As an alternative, you can create your own link() or url() function, which calls HtmlHelper::link and always adds base = false.
For this to work, you must also have a .htaccess in the document root, which rewrites the requests to their real destination
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /_cake/$0 [L]
You must also pay attention to requests destined for the _wp directory.

Resources