Apache .htaccess redirect file - apache

I have written this rule in the .htaccess file:
# Rewrite super sized ciim5 images to large
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ciim5/([0-9]+)/([0-9]+)/super_000000\.jpg$ /ciim5/$1/$2/large_000000.jpg [NC,L,R=301]
What it should do is when the path /ciim5/123/456/super_000000.jpg is requested, it should 301 redirect to the same path, but serve large_000000.jpg instead. Both super_0000000.jpg and large_000000.jpg are actual physical files on that server (effectively we want to hide super sized images from public and redirect them to large)
I've tried this rule on my localhost (Apache/2.4.7 (Ubuntu)) and it worked (rest of the .htaccess file is same as the production one). But when I upload it onto the production server (Apache/2.4.18 (Ubuntu)), it's not doing anything at all. But the previously existing rewrites do work (= mod_rewrite is enabled). I have made sure the .htaccess file is updated on the production site (and it is), and even made a test.html file and open it in the browser just to make sure I'm uploading files in the correct directory.
I'm now sitting here scratching my head, can someone tell me why the above doesn't work on the production server and how to get it to do the redirect?
Edit:
The path /ciim5 is actually setup as an alias to a directory that's not in the web root. So could this be the case of the .htaccess file in the web root not being read? Should I put the .htaccess file in the /ciim5 directory instead?
Edit 2:
Enabling logs shows this:
[Thu Dec 01 10:05:48.444800 2016] [rewrite:trace1] [pid 4572] mod_rewrite.c(476): [client *:33116] * - - [*.org.uk/sid#7f767fb185c8][rid#7f767fa9c0a0/initial] pass through /ciim5/295/491/super_000000.jpg
[Thu Dec 01 10:05:48.445015 2016] [rewrite:trace3] [pid 4572] mod_rewrite.c(476): [client *:33116] * - - [*.org.uk/sid#7f767fb185c8][rid#7f767fa9c0a0/initial] [perdir /static-media/] strip per-dir prefix: /static-media/295/491/super_000000.jpg -> 295/491/super_000000.jpg
[Thu Dec 01 10:05:48.445113 2016] [rewrite:trace3] [pid 4572] mod_rewrite.c(476): [client *:33116] * - - [*.org.uk/sid#7f767fb185c8][rid#7f767fa9c0a0/initial] [perdir /static-media/] applying pattern '^(ciim5/[0-9]+/[0-9]+/)super_000000\\.jpg$' to uri '295/491/super_000000.jpg'
[Thu Dec 01 10:05:48.445204 2016] [rewrite:trace1] [pid 4572] mod_rewrite.c(476): [client *:33116] * - - [*.org.uk/sid#7f767fb185c8][rid#7f767fa9c0a0/initial] [perdir /static-media/] pass through /static-media/295/491/super_000000.jpg
What's this "strip per-dir prefix" thing? Seems like it removes ciim5 or static-media (the actual folder which ciim5 is aliased to). So I should actually remove the ciim5 part from my rewriterule?

So here's what I had to do:
My webroot is in /var/www/*.org.uk. But the media files were in /static-media in the root directory. I had to put an .htaccess file in /static-media and put the following directive:
RewriteRule ^([0-9]+/[0-9]+/)super_000000\.jpg$ /ciim5/$1large_000000.jpg [NC,L,R=301]
Seems like when the request comes to Apache, the strip per-dir prefix (I don't know what this is!) seems to remove /static-media and /ciim5 directory prefixes, so the incoming request (lets say /ciim5/123/456/super_000000.jpg) the .htaccess file in /static-media receives is: /123/456/super_000000.jpg. So now I need to redirect that to the same path but with /ciim5 prefix and to large_000000.jpg. This was done by trial and error really, and I don't fully understand why this works the way this works. So please expand on this answer.

Related

Apache no matching directory

Why am I having this no matching directory, but I have one that was working the other day, but now it isn't. I'm using Codeigniter.
[Sun Feb 19 11:43:03.077759 2017] [autoindex:error] [pid 5204:tid 1864] [client ::1:51750] AH01276: Cannot serve directory C:/xampp/htdocs/Groupie/assets/files/: No matching DirectoryIndex (index.php,index.pl,index.cgi,index.asp,index.shtml,index.html,index.htm,default.php,default.pl,default.cgi,default.asp,default.shtml,default.html,default.htm,home.php,home.pl,home.cgi,home.asp,home.shtml,home.html,home.htm) found, and server-generated directory index forbidden by Options directive, referer: http://localhost/Groupie/newsfeed
It seems that in C:/xampp/htdocs/Groupie/assets/files/ you don't have a file that can be served as a directory index when that directory is visited. Add one of the files in the list:
index.php,index.pl,index.cgi,index.asp,index.shtml,index.html,index.htm,default.php,default.pl,default.cgi,default.asp,default.shtml,default.html,default.htm,home.php,home.pl,home.cgi,home.asp,home.shtml,home.html,home.htm

Trying to force a RewriteRule Redirect with status code 200 for missing file

Don't get angry right away :P, I am exploring options and I am having a reason for looking into this.
I (can) have script files in a folder /scripts/competitions/*.js.
Now, a script files will be written in this folder by a serverside script.
So at some point, a specific competition script file might not be available.
So, the website trying to load that script file gets a nice 404.
Now, I can leave it at that, unless you open Dev Tools you won't notice the 404, right?
But, I rather have my server always return a 200 for that specific folder, whatever the file(s) existence.
For this url: /scripts/competitions/A9ACCC8F-B78A-4E0D-8023-51D8F56B5C55.js
My current rewrite code loads the file fine, but doesn't change the status code tot 201 ( just a test).
ReWriteRule ^/scripts/competitions/([^.]*).js$ /scripts/competitions/$1.js [R=201]
So I reckon the regex is not catching correctly.
When I change it to
ReWriteRule ^scripts/competitions/([^.]*).js$ /scripts/competitions/$1.js [R=201]
It does redirect to 201 but give me an error message under the Created heading
Created
The server encountered an internal error or misconfiguration and was
unable to complete your request.
Please contact the server administrator at webmaster#localhost to
inform them of the time this error occurred, and the actions you
performed just before this error.
More information about this error may be available in the server error
log.
Apache/2.4.7 (Ubuntu) Server at tracker.test.local Port 80
So that one is catching, redirecting but not loading the script file IF it does exists (it did exist for that error message test).
So what I am after is a normal 200 response plus the JS body when it exists and 200 plus empty body (most preferred if possible) or the error message that file cannot be found.
A dump from my log for the first rewrite rule (starting with ^/):
[Tue Nov 24 20:51:40.594882 2015] [rewrite:trace3] [pid 3593:tid 139723926361856] mod_rewrite.c(468): [client 192.168.1.6:55261] 192.168.1.6 - - [tracker.test.local/sid#7f14130f5618][rid#7f14130540a0/initial] [perdir /var/www/test/tracker/root/] strip per-dir prefix: /var/www/test/tracker/root/scripts/competitions/A9ACCC8F-B78A-4E0D-8023-51D8F56B5C55.js -> scripts/competitions/A9ACCC8F-B78A-4E0D-8023-51D8F56B5C55.js
[Tue Nov 24 20:51:40.595077 2015] [rewrite:trace3] [pid 3593:tid 139723926361856] mod_rewrite.c(468): [client 192.168.1.6:55261] 192.168.1.6 - - [tracker.test.local/sid#7f14130f5618][rid#7f14130540a0/initial] [perdir /var/www/test/tracker/root/] applying pattern '^/scripts/competitions/([^.]*).js$' to uri 'scripts/competitions/A9ACCC8F-B78A-4E0D-8023-51D8F56B5C55.js'
[Tue Nov 24 20:51:40.595313 2015] [rewrite:trace1] [pid 3593:tid 139723926361856] mod_rewrite.c(468): [client 192.168.1.6:55261] 192.168.1.6 - - [tracker.test.local/sid#7f14130f5618][rid#7f14130540a0/initial] [perdir /var/www/test/tracker/root/] pass through /var/www/test/tracker/root/scripts/competitions/A9ACCC8F-B78A-4E0D-8023-51D8F56B5C55.js
Hope that some one can push me in the right direction.
*update
Dump from log when file does exist and it shows a 201 but also the message that it encountered an internal error
[Tue Nov 24 21:27:31.134452 2015] [rewrite:trace3] [pid 3593:tid 139723842434816] mod_rewrite.c(468): [client 192.168.1.6:55684] 192.168.1.6 - - [tracker.test.local/sid#7f14130f5618][rid#7f14130440a0/initial] [perdir /var/www/test/tracker/root/] strip per-dir prefix: /var/www/test/tracker/root/scripts/competitions/A9ACCC8F-B78A-4E0D-8023-51D8F56B5C55.js -> scripts/competitions/A9ACCC8F-B78A-4E0D-8023-51D8F56B5C55.js
[Tue Nov 24 21:27:31.134479 2015] [rewrite:trace3] [pid 3593:tid 139723842434816] mod_rewrite.c(468): [client 192.168.1.6:55684] 192.168.1.6 - - [tracker.test.local/sid#7f14130f5618][rid#7f14130440a0/initial] [perdir /var/www/test/tracker/root/] applying pattern '^scripts/competitions/([^.]*)\\.js$' to uri 'scripts/competitions/A9ACCC8F-B78A-4E0D-8023-51D8F56B5C55.js'
[Tue Nov 24 21:27:31.134495 2015] [rewrite:trace2] [pid 3593:tid 139723842434816] mod_rewrite.c(468): [client 192.168.1.6:55684] 192.168.1.6 - - [tracker.test.local/sid#7f14130f5618][rid#7f14130440a0/initial] [perdir /var/www/test/tracker/root/] forcing responsecode 201 for /var/www/test/tracker/root/scripts/competitions/A9ACCC8F-B78A-4E0D-8023-51D8F56B5C55.js
Use server-side script as a fallback when the file doesn't exist. Also, leading directories, including slash, are stripped from .htaccess rules.
RewriteCond %{REQUEST_URI} !-f
RewriteRule ^(scripts/competitions/)[^./]*\.js$ $1not_exist.php [L]
It doesn't have to be PHP, it could just be a static js file so it's served with the right MIME type. You just need something to return default content. Put the default in the same directory as your competition files and refer to it in your rule. The content could be:
<?php
header('Content-Type: application/javascript');
echo '// ', basename($_SERVER['REQUEST_URI']), ' does not exist yet.';

Why are requests being made to /eyeblaster on my website?

My Adsense-supported website's error.log file has lots entries like this which I'm fairly sure are advert related:
[Fri Apr 12 07:19:57 2013] [error] [client IP] File does not exist: /var/www/[mywebsite]/htdocs/eyeblaster, referer: http://apac-bidder.mathtag.com/notify/iframe? [snip lots of junk]
What is it and is it harmful?
Should I create an empty eyeblaster.html file to shut it up?
I also have another two that are like it - are they related?:
[Fri Apr 12 07:08:52 2013] [error] [client IP] File does not exist: /var/www/[mywebsite]/htdocs/7196176924447058959
[Fri Apr 12 07:13:58 2013] [error] [client IP] File does not exist: /var/www/[mywebsite]/htdocs/_sans
Take a look at StackOverflow question above - Determine which advertisement made a request to /eyeblaster/addineyev2.html. I think all will be revealed :).
Eyeblaster is an online advertising company now called Sizmek (formerly Mediamind).
As John mentioned, this file is used as an iframe buster, just like DoubleClick's iframe buster, which should be placed by a publisher on [www.example.com]/doubleclick/DARTIframe.html
Another solution is to disallow eyeblaster.
Just add these to your robots.txt:
Disallow: /eyeblaster
Disallow: /addineyeV2.html
Or I prefer to redirect eyeblaster to index.html inside .htaccess file
RewriteCond %{REQUEST_URI} (eyeblaster|addineyeV2) [NC]
RewriteRule ^(.*)$ /index.html? [R=301,L]
eyeblaster is just another piece of malware which most machines end up getting on at one point from what I can tell it hasn't caused any series issues but it is best to remove it from your machine whenever your anti-malware software pics it up

mod_rewrite makes error logs fill up

I am using apache's mod rewrite to make "pretty urls", here is an example of a rule:
RewriteRule ^articles/(.+?)\.([0-9]+)/page\=([0-9]+)?$ /index.php?module=articles_full&aid=$2&title=$1&page=$3 [N,NC,QSA]
Problem is my error log fills up with stuff like this:
[Fri Feb 01 20:36:11 2013] [error] [client 94.246.88.189] File does not exist: /home/gamingon/public_html/articles
[Fri Feb 01 20:35:55 2013] [error] [client 66.249.73.195] File does not exist: /home/gamingon/public_html/articles
[Fri Feb 01 20:34:39 2013] [error] [client 66.249.73.195] File does not exist: /home/gamingon/public_html/articles
What would be the best way to stop the errors? It seems to work okay though but still i don't think the error logs are supposed to fill with that?
Add a RewriteCond that will make the RewriteRule kick in only when the file doesn't exist:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^articles/(.+?)\.([0-9]+)/page\=([0-9]+)?$ /index.php?module=articles_full&aid=$2&title=$1&page=$3 [N,NC,QSA]
Edit:
Looking at your rewrite rule again, it's a very specific regex that will only match when the uri is in the format
The text "articles"
one or more of any character
a dot
one or more digits
the text "/pages="
one or more digits
So, it will match on a URL like:
http://example.com/articles/awesome-article.3/page=2
but not on a URL like:
http://example.com/articles/awesome-article/page=2.
If you're seeing 404s, it's likely people are trying to visit URLs of the second form. Could be that your site has some links that are not being created correctly, check the logs to see if you can find any referers to help track down the problem.
I tested your rule and it works fine for me when I give it a correctly formatted URL.

Help Needed With (I thought) Basic mod_rewrite Setup

I run my own server at home. I am attempting to do a mod_rewrite but I cannot seem to get it to work correctly.
Say my url is "http://www.site.com/". I have a user set up, whose public_html directory is serving pages; let's say that url is "http://www.site.com/~myUser/theDirectory/". What I am trying to do is set it so that when you type in "http://www.site.com/theDirectory/" it will actually serve the pages out of "http://www.site.com/~myUser/theDirectory/", but look like its coming from "http://www.site.com/theDirectory/". I edited my /etc/apche2/sites-enabled/default file and added these lines:
RewriteEngine On
RewriteRule ^theDirectory/$ /home/myUser/public_html/~myUser/theDirectory/
I also tried various versions of the rule:
RewriteRule ^/theDirectory/$ /home/myUser/public_html/theDirectory/
RewriteRule ^/var/www/html/theDirectory/$ /home/myUser/public_html/theDirectory/
I also made sure that the rewrite module was enabled. At first, I was getting this error:
[Fri Jun 17 18:11:35 2011] [error] [client xxx.xxx.xxx.xxx] File does not exist: /var/www/theDirectory
So I created that file, and now I am getting this error:
[Fri Jun 17 23:15:45 2011] [error] [client xxx.xxx.xxx.xxx] Directory index forbidden by Options directive: /var/www/theDirectory/
So, I'm not really sure where to go from here. Any and all advice will be appreciated. Thanks for taking the time to read.
Have a great day :-)
Add the following to your virtual host configuration:
RewriteEngine on
RewriteRule ^/theDirectory(/.*)$ /home/myUser/public_html/theDirectory$1 [L]