htaccess image redirection - php

I have 20 million images spread across 3 subdomains. Here is an example:
subdomain3.domainabc.com/images/image5.jpg
I do not wish to move these images to my new server/domain and I do not wish to link to them directly. Instead, I want to use my htaccess file on domainxyz.com. So, on domainxyz.com when this image is called:
subdomain3.domainxyz.com/images/image5.jpg
It goes to server domainabc.com and displays the correct image. Is this possible? Here is what I've put into domainxyz.com htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.+)\.domainxyz\.com$
RewriteRule ^(.*)\.jpg$ http://%1\.domainabc\.com/$1\.jpg [R,L]

Take a look at: Mod-rewrite rule for external pages?
Tunneling is not possible; only redirecting.

Related

Creating SEO urls for resized images via htaccess

I have problem with creating .htaccess rule for creating url for small resized images.
I have saved images inside upload/images/ directory and it looks like this:
somedomain.com/upload/images/bigimage-some_name123.jpg
I have already created url via PHP so i can call resized image:
somedomain.com/upload/images/small/bigimage-some_name123-150x100.jpg
From this small url I need 4 parameters:
name: bigimage-some_name123
extension: .jpg or jpg
width: 150
height: 100
Via .htaccess need to create url so I can fetch all parameters above and show resized image on the fly:
somedomain.com/image-resizer/index.php?image=upload/images/bigimage-some_name123.jpg&width=150&height=100
Colleague of mine send me this .htaccess line and I need to rewrite it so it can work on my project.
RewriteRule
^upload/images/small/([^/.]+)-([0-9]{3,5})([^/.]+)-([0-9]{2,3})x([0-9]{2,3}).jpg$
module/show_image.php?name=../files/$2$3.jpg&width=$4&height=$5 [L]
This can be well handled in rewrite rules and it is much faster than doing in PHP also. You can use this rule in root .htaccess:
RewriteEngine On
RewriteBase /
RewriteRule ^(upload/images)/small/(.+?)-(\d+)x(\d+)\.(jpe?g)$ image-resizer/index.php?image=$1/$2.$5&width=$3&height=$4 [L,QSA,NC]
You should not try to extract information from the path in the .htaccess rewrite rule. It's much easier doing that in PHP. So what I do is to feed the whole SEO optimized URL to the PHP page behind the rewrite rule. My .htaccess looks like this:
RewriteRule ^(.*)$ /page.php?SEOPath=$1 [NC,L,QSA]
You can see how simple the rewrite rule now is. Then in PHP I work on the SEO path, and extract the wanted information, which is not too difficult.
Of course you still need to adapt this to your situation. For instance, restrict it to .jpg files only. In your case it could be:
RewriteRule ^(.*).jpg$ /module/your.php?SEOPath=$1 [L]

Moving a HTML site to PHP

I currently run a site with 750 pages of .html webpages (yeah I know it was a stupid idea, but I'm a novice). I'm looking to move these to php. I don't really want to set up 750 individual 301 redirects and rewrite each page to .php
I've heard that I can use htaccess to this. Anyone know how?
A few additional questions -
Can I permanently redirect these links from html to php without losing my search engine rankings and
if I want to add php to each of the files (i.e. a php file menu (using the include command) to make the links quicker to update will this work? Because won't they still be html files?
Sorry for the stupid questions, but I'm still learning.
Congratulations on a 750 page site - you must have put some work into that.
To collect your current list of pages use a tool called xenu to create an export into excel. You can then easily change the name the files to PHP in column b and create a .htaccees file.
However why would you want 750 php files? If you have lots of data pages, make it one page and suck in the HTML main content and reference one page. If you have a page called warehouse-depot-22-row-44.html then change that to show-warehouse-row.php?depot=22&row=44 and return that content only. This will significantly reduce your number of pages and to start using databases to render the content.
For redirecting you could use the Apache Module mod_rewrite: https://httpd.apache.org/docs/current/mod/mod_rewrite.html
You can use url rewriting to match a specific file name request with a regular expression and then decide where to redirect if matched
RewriteRule ^myname/?$ myname.php [NC,L]
http://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/
Depends on the structure you have.
You want the user to access them in their natural location?
/public_html/folder1/file.php
user would access like
mydomain.com/folder1/file
or you want to map them differently?
Personally I think I would use a rewrite rule to map all requests to my /public_html/index.php and would map the requests from there using php (using include for instance). This gives great flexibility, plus you have a single point of entry for your application which is very beneficial since you can easily maintain control of the application flow.
The .htaccess would look like this
#
# Redirect all to index.php
#
RewriteEngine On
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# RewriteCond %{REQUEST_URI} !^/index\.php
# RewriteCond %{REQUEST_URI} (/[^.]*|\.(php|html?))$ [NC]
RewriteCond %{REQUEST_URI} (/[^.]*|\.)$ [NC]
RewriteRule .* index.php [L]
of course I place all my not directly accessible files (everything except index and css, js, images, etc) to a folder outside the public_html to ensure no user can ever access them directly ;)
I've had a similar (yet much much smaller) site that went through the same thing.
I have this in my .htaccess:
RewriteEngine On
RewriteRule ^(.*)\.html$ $1.php [L]
This will help redirect any visitors to your .html addresses to your .php addresses.
You hopefully have an IDE (I recommend Aptana), and you can use some of the find/change functions project-wide, and hopefully with some time and patience get your internal links from .html to .php.
But, I caution you a little bit - Perhaps it is time to look into a database based CMS, such as Wordpress or Drupal?

Redirect based on url arguments (via PHP or cPanel)

I just started using Amazon S3 to host static files (images, videos, etc.).
For accessing the uploaded files, temporary links are created.
A temporary link looks like this one:
http://zeebit.s3.amazonaws.com/stackoverflow-logo.png?AWSAccessKeyId=AKIAIXEHEYSBDWAAXVVA&Expires=1346888760&Signature=B%2BS%2FlUoRXno3UfSqf9Ua0RuCcBc%3D
What I want is to serve these file through my url, something like this:
http://s3.mydomain.com/zeebit/stackoverflow-logo.png/AKIAIXEHEYSBDWAAXVVA/B%2BS%2FlUoRXno3UfSqf9Ua0RuCcBc%3D
I know I can redirect requests to http://s3.mydomain.com to the Amazon url via PHP (for example), but I don't want the address bar to change.
I can create a .htaccess to transform the url to the Amazon url, but as I know .htaccess can't redirect to external resources.
So, how can I solve this?
There are a couple of solutions:
.htaccess Solution #1 - Rewrite Rule
RewriteEngine On
RewriteCond %{HTTP_HOST} ^s3\. # Hostname starts with "s3."
RewriteCond %{REQUEST_URI} !-f # Not a file
RewriteCond %{REQUEST_URI} !-d # Not a directory
RewriteRule ^/(.+)/(.+)/(.+)/(.+)/(.+)$ http://$1.s3.amazonaws.com/$2?AWSAccessKeyId=$3&Expires=$5&Signature=$4 [R=302,L]
NOTE: Your initial desired URL was missing the "Expires" value, so the above would work for URLs formed like so:
http://s3.yourdomain.com/[[S3_Bucket_Name]]/[[S3_Filename]]/[[AWSAccessKeyId]]/[[Signature]]/[[Expires]]
So:
http://s3.mydomain.com/zeebit/stackoverflow-logo.png/AKIAIXEHEYSBDWAAXVVA/B%2BS%2FlUoRXno3UfSqf9Ua0RuCcBc%3D/1346888760
would redirect to
http://zeebit.s3.amazonaws.com/stackoverflow-logo.png?AWSAccessKeyId=AKIAIXEHEYSBDWAAXVVA&Expires=B%2BS%2FlUoRXno3UfSqf9Ua0RuCcBc%3D&Signature=1346888760
.htaccess Solution #2 - Redirect
Whilst being a less flexible solution than the above, you could put the following into your .htaccess file
redirect 302 /s3/ http://zeebit.s3.mydomain.com/
Using this rule, requests for
http://yourdomain.com/s3/stackoverflow-logo.png?AWSAccessKeyId=AKIAIXEHEYSBDWAAXVVA&Expires=B%2BS%2FlUoRXno3UfSqf9Ua0RuCcBc%3D&Signature=1346888760
Would basically retain everything after /s3/ and simply replace everything preceeding it with the Amazon S3 location.
http://zeebit.s3.amazonaws.com/stackoverflow-logo.png?AWSAccessKeyId=AKIAIXEHEYSBDWAAXVVA&Expires=B%2BS%2FlUoRXno3UfSqf9Ua0RuCcBc%3D&Signature=1346888760

Mask outgoing links

On a php web page, i'm displaying an html5 player which plays an mp3 track, and there's a download button next to that. I want to mask the download link only, so visitors will not be able to see the directory tree of my server (www.example.com/content/plugins/audio/downloads/test.mp3). I want to change the link to something more simple and smaller like (www.example.com/download/test.mp3) without changing the folders structure of my server.
I'm not sure if it is an .htaccess or PHP thing.
Thanks in advance.
With .htaccess you would need the following rewrite rule:
$ pwd
/var/www
$ cat .htaccess
RewriteEngine on
RewriteRule ^/download/(.*\.mp3) /content/plugins/audio/downloads/$1 [NC]
It will match all http://yourdomain.com/download/….mp3 requests and redirect to http://yourdomain.com/content/plugins/audio/downloads/….mp3
If you want your .htaccess to work from inside a subdirectory, use RewriteBase:
$ cat subdirectory/.htaccess
RewriteEngine on
RewriteBase /subdirectory
RewriteRule ^download/(.*\.mp3) content/plugins/audio/downloads/$1 [NC]
i.e. http://yourdomain.com/subdirectory/download/….mp3 to http://yourdomain.com/subdirectory/content/plugins/audio/downloads/….mp3

Block certain site from accessing a certain file

I have player.php file which calls the video player to play a certain video. How can i block certain sites from accessing this file and using it to embed videos on there site. In other words What code can i use inside player.php to block certain sites from accessing this file only.
You can do this on three levels.
1) Web server
For instance, using .htaccess file if you're on an Apache server.
This could be done with a rewrite that pushes them to some dummy file or 404 or whatever you like. For example:
RewriteEngine on
# Options +FollowSymlinks
RewriteCond %{HTTP_REFERER} badsite\.com [NC]
RewriteRule .* - [F]
This is really the ideal way because it precludes the need to interpret PHP.
2) PHP
In your page, use the $_SERVER['HTTP_REFERER'] (which may not be set if there is no referrer) and search for the domain in question in the string.
This is second best, and may be your only option if you can't alter the Apache configuration.
3) Javascript
Doesn't really prevent access to anything, because the check happens client-side (they've downloaded player.php and the Javascript itself prior to running it). If they went directly to the video or whatever, it wouldn't stop them from getting the file. You would use the document.referrer and search for the domain as with the PHP example.
If you are using Apache and have access to your .htaccess file, I suggest you use that instead. This page is an excellent resource.
You could try something like this, assuming player.php is in your web root:
RewriteEngine On
RewriteCond %{HTTP_REFERER} ^player\.php.*
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?your-domain\.com/ [NC]
RewriteRule .* http://your-domain.com/please-dont-steal\.php[NC]
You're better off dealing with this issue server side, so PHP is a good bet. You'll need to examine the HTTP referrer header to see whether you're being hotlinked.
there are lots of tricks you can do with Apache mod-rewrite and/or .htaccess

Categories