I have got following folder structure for my domain (domain.com):
/data/images (it´s subdomain name, so it´s accessable via http:// images.domain.com)
/data/www (it´s subdomain name, so it´s accessable via http:// http://www.domain.com)
/data/.htaccess
And I´m trying to do:
If some image in images.domain.com doesn´t exist, rewrite URL with has request to http://www.domain.com/somescript.php, so for example
http://images.domain.com/non-existing-image.jpg
should be rewritten to
http://www.domain.com/somescript.php?id=non-existing-image.jpg.
Here is my htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} \.(jpg|gif|png|bmp)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /data/www/somescript.php?id=$1 [L,QSA]
But it´s not working. Browser said: “not found”. If I tried to change path to “somescript.php” the results were “bad-request” or “not-found”.
Via access log I saw that in most cases the path to “somescript.php” is starting from “/data/www/images/” (so final path is non-existing “/data/www/images/www/somescript.php”) so I guess I need to get one level higher in my folder structure, but I don´t know how (I guess I´ve already tried almost all combinations).
Could someone please help me how to properly set the combination of RewriteBase and absolute/relative path for the “somescript.php” file?
I also want to note, that if I change the path to “http://www.domain.com/somescript.php” on the last line in my htaccess file, it works well – but due to “http://” it´s redirected and I would like to made as rewrite.
Thank you very much.
Try replacing the
/at the beginning of your rewrite rule. In order for it to actually go to another domain, you must specifyhttp://before the domain, so your last line would need to be like this:If you don’t specify the
http://it’s seen as a relative path and will go off of the domain currently in use, just appending it as a regular relative path.Or: (This only works on some setups)
Apache (or whatever you’re using) doesn’t go off of your user’s home directory. It uses the root directory for the website. So by specifying the
/at the beginning of the domain, you’re actually saying/www/images.domain.com/<rest of absolute path>. However, if both these directories are owned by the same user, you can use a relative path to go up to the parent directory and then into the alternative directory, like so:I wouldn’t recommend this much though, as relative paths can easily become broken.