CDN Related : Rewrite Image URLs automatically from .htaccess - image

Recently I bought a CDN and set it up. In my site, the images are stored in a folder named 'images' and the Image urls are obviously linked in this manner. (*Ex : images/some_image.png*)
Since I want to use the CDN right way, I need to rewrite the urls without having to manually change each and every image path.
I tried an .htaccess code which was suggested for a similar problem
RewriteEngine On
RewriteBase /
RewriteRule ^images/(.*)$ http://cdn.mydomain.com/$1 [L,R=301]
But that didn't seem to work properly as all the images were linked improperly.
So I would like to know the changes in this code. Any response would be appreciated.

This should work:
RewriteCond %{HTTP_HOST} ^yourdomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.yourdomain\.com$
RewriteRule ^images\/?(.*)$ "http\:\/\/cdn\.yourdomain\.com\/$1" [R=301,L]
However, please note that this is just a temporary solution!In order to get the max out of your CDN, you need manually point images to your CDN in order to save one HTTP for every image.

Related

rewrite url only and stay on the same page

I have read a lot on stack about rewriterule and how it applies and I've tried reading up on some good articles online but I still cannot wrap my head around a few things.
I have blogs setup where all folders are in
https://domain.ca/posts/post-tree/*
So I've setup htaccess like this
RewriteRule ^posts/post-tree/(.*)$ /index.php?$1 [R=301]
As I'm sure you can guess this basically brings me root index.php where I catch this request with a $_GET to know the name of the blog folder it was requesting.
This is fine I can hit index.php and with $_GET I know the blog page they requested.
What I do not get, and I've tried a lot of things, is once I have this request in index.php how do I re-write the URL to show something like https://domain.ca/blogpage/ instead of looking like https://domain.ca/index.php? where https://domain.ca/blogpage/ does not really exist of course, but it is because I want to hide the http://domain.ca/posts/post-tree/ path.
Its a little like when wordpress processes a blog page with the id and after rewrites the url to whatever slug is set for that blog page. at least my understanding of it as they don't have individual folders for blogs, but I do.
I finally got this working with the following in the htaccess file
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# the above checks if file or folder exists, if not the below is processed
# this will route to base index file and fetch $1 folder via $_GET
RewriteRule . /posts/post-tree/index.php?$1

Migrating a CodeIgniter site to a different domain and moving the whole thing down one level in directory

I am working on a site, which is built with CodeIgniter.
I have never used the framework before...I've learnt a little bit in the last couple days but really cannot solve my problem.
I have downloaded the site and I want to upload it to my own domain so not to make changes to the live site before testing.
the original site is example.com.
I want to upload it to mysites.com/examplesite (this will be the base - I use this domain for all my clients' test sites).
I have moved all the files and the database is in place.
I have found the database configuration file and have successfully edited it.
I am having trouble getting the site to work, I think it must be because of the site now being down a directory level.
I have edited $config['base_url'] so that it is correct...I am pretty sure it is correct because I can now reach the homepage where I couldn't before editing that variable. But, i can only reach the homepage - when I click on a link to another page, I get 500 error Internal server error.
Does anyone have a quick fix for me?
Thanks!
Keep this code on your project's root .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /examplesite/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

IsapiRewrite rules generate strange pages/folders?

i'm using IsapiRewrite from helicontech for my custom classic ASP shopping cart.
My actual httpd.ini file is:
[ISAPI_Rewrite]
RewriteBase /
RewriteCond %HTTPS off
RewriteCond Host: (?!^www.domain.com)(.+)
RewriteRule /(.*) http\://www.domain.com/$2 [I,RP]
RewriteRule /httpd(?:\.ini|\.parse\.errors).* / [I,O]
RewriteRule /(.+)-([^-]*)-([^-]*)-([^-]*)\.html$ /detail.asp?brand=$1&model=$2&id=$3&lg=$4 [L]
So i'm actually using this component only to rewrite the product detail page in a nice way.
Lately looking in google cache i find some folders with files inside cached by googleBot that doesn't exist on my server eg: domain.com/carrera-CHAMPION ... and so on.
The page drawn if i access these strange pages is the detail.asp page but without css and some js loaded.
I tested recently the site with Acunetix scanner and seems like he found too these strange folders.
First i added a javascript check to se if Jquery not loaded then that means the page is one of these so i throw an alert and redirect user to homepage ..
Do you know what could be ? or how i could solve this via a rule in httpd.ini?
Hope i make myself clear if no feel free to ask me details,
thank you
Can you confirm that when you disable ISAPI_Rewrite these strange pages/folders disappear?
Please note that ISAPI_Rewrite only rewrites response headers and can't generate anything, so it's highly unlikely to be the cause of the issue.
BTW, your config is the mixture of ISAPI_Rewrite 2 and 3 syntax. If it's v2, please fix it like this:
[ISAPI_Rewrite]
RewriteCond %HTTPS off
RewriteCond Host: (?!www.domain.com).+
RewriteRule /(.*) http\://www.domain.com/$1 [I,RP]
RewriteRule /httpd(?:\.ini|\.parse\.errors).* / [I,O]
RewriteRule /(.+)-([^-]*)-([^-]*)-([^-]*)\.html$ /detail.asp\?brand=$1&model=$2&id=$3&lg=$4 [L]

.htaccess image redirection for specific images on one to one basis

I used to have a site up and running on a domain of mine, that I have since taken down. However in that site I had a handful of specific images that I had either shared with other sites.
Its been a while since I visited the logs of that domain, and I realize that there is about 2 dozen images coming up as 404, and I recognize them as the images shared. What I want to do is put those images on another domain of mine, and have any requests coming for those images get rewritten with the other domains url where the images now reside.
Is this possible, to specify images on a one to one basis and have them point to there counter parts on the other domain? I've seen it done where people are being not so selective but nothing in terms of what I would like to do.
Yes, you can do that with .htaccess.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
Options +FollowSymLinks
RewriteRule ^img/path/to/file1.jpg$ http://newdomain.com/new/path/to/file1.jpg [L,R=301]
RewriteRule ^img/path/to/file2.jpg$ http://newdomain.com/other/new/path/to/file2.jpg [L,R=301]
RewriteRule ^img/path/to/(file3.jpg|file4.jpg)$ http://newdomain.com/typical/new/path/to/$1 [L,R=301]
</IfModule>
Considering http://olddomain.com/img/path/to/file1.jpg is one of the image files and the .htaccess is in the document root of olddomain.com, this should do the job. If you can't match the old and new file names against some regular expressions you'd need to have a line with RewriteRule for each missing image.

Protecting Magento Images

It is good that magneto offers watermarking of images so a watermarked image will appear as
http://www.yourwebsite.com/media/catalog/product/cache/1/image/de684549e4151748e56ee0dd7902c19f/m/y/my-first-image.jpg
This is good. But if anyone removes the cache/1/image/de684549e4151748e56ee0dd7902c19f part of URL so that it becomes
http://www.yourwebsite.com/media/catalog/product/m/y/my-first-image.jpg
He can view the image and copy it. What is best way to cope up with this problem?
This is how I managed to do it.
Put an .htaccess file in media/catalog/ folder in Magento with following code in it
Options +FollowSymLinks
RewriteEngine on
#Following line allows the actual images to be accessed by admin end directly
RewriteCond %{HTTP_REFERER} !^http://www.yourwebsite.com/.*$ [NC]
#Following line allows the watermarked images to be accessed directly. Rule says that if URL does not contain cache
RewriteCond %{REQUEST_URI} !(/cache/) [NC]
#This is the page where visitor will be redirected if tries to access images directly.
RewriteRule \.(gif|jpg)$ http://www.yourwebsite.com/do-not-try-to-steal-images.html/ [R,L]
Well, create yourself your images with the watermark !!! Like that if the cache is deleted, the cache will recreate an image from the already watermarked image
You can create an .htaccess file in the media/catalog folder with the following lines in it:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*?/cache.*)$ $1 [L]
RewriteRule ^(.*)$ $1 [F]
This will return a 403 to the user if they try to view the original file but let them see the images that are in the cache.
This breaks previewing the images in the admin as they display the original files and not a generated thumbnail.

Resources