htaccess redirect raw image viewing - image

I'd like to redirect any image that is viewed directly to a handler page:
if http:// mysite.com/pics/filename.jpg (or www.)is in the URL
then redirect to http:// mysite.com/pics/index.php?img=filename.jpg (no www)
But if the image is being called by a webpage or a mobile html5 app anywhere then it should be served per normal.
So if mypage.html contains an img tag with the direct photo in it it will be shown in that page. But if http:// mysite.com/pics/filename.jpg is the url then it should redirect. In other words if the file is being viewed directly it should redirect to the wrapper page, but if it's already in a wrapper page (anywhere) it shouldn't redirect.
I've seen various redirect code but none that that references the visible url for the if statement, so I don't know how to do this. And the ones I've found and tried don't work, either redirecting all requests, or not doing anything
Thanks!

This will take care of people hotlinking to images on your site for you:-
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?mysite.com [NC]
RewriteRule ([^/]+)\.(jpg|jpeg|png|gif)$ pics/index.php?img=$1.$2 [NC,L,R]
As for your mobile HTML5 app you are going to need some other way to identify it to your web server. So you are going to have to see what referer that says it is when it is asking for images. It should be pretty easy for you to write a PHP script to tell you that, then you just need to add to the above code to include that.
Simon

Related

How to do 301 redirect in herokuapp free hosting?

I am trying to do 301 redirect in my herokuapp. How can I do it. I have used <link rel="canonical" href="http://mysite.herokuapp.com">. And I have also used this tag for every page with the url of the page. Still I failed to fix 301 redirections problem in my heroku website.
With url http://mysite.herokuapp.com website is working but with url
www.http://mysite.herokuapp.com it shows
This site can’t be reached
Yoast seo shows this message
"Warning, no 301 redirects are in place to redirect traffic to your
preferred domain. Pages that load successfully both with and without
www. are treated as duplicate content! "
For this problem my website is not indexing in google, I think. How can i show my website in google search.
Note: I have submitted my website to google search console and verified it.
Please help me to solve this problem.
You will want to use something called htaccess
There should be a file in the root of your web directory called ".htaccess" If not create one.
You want a rule something like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.mysite.herokuapp.com [NC]
RewriteRule ^(.*)$ http://mysite.herokuapp.com/$1 [L,R=301]
This should redirect www traffic to a none www prefix

.htaccess image to web page redirect of sorts

I think this can be done, though not sure, so I came here to find out from you experts. I researched extensively, but always get the opposite of what I want to do. I was wondering if someone types in the image file URL (on server side) into an address bar if that could be directed to its corresponding web page.
For example:
One types in yourdomain.com/wp-content/uploads/image-name.jpg and it's directed to yourdomain.com/print/image-name (w/o .jpg)
So instead of one just seeing the image in a window, they will see the image, it's information and how to purchase it on my brand web site.
If that is not possible or the file name doesn't match, can directly typed server side image file URLs redirect to my website galleries or home page? And where in .htaccess would this code be located? Above begin wordpress or not?
If above Begin WP, will that interfere with this code that already exists? If so how would I exactly write it?
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.sitename.com/$1 [R,L]
Thanks in advance!
EDIT: To clarify, my site being down has nothing to do with the help I received here. I upgraded my web host account and there was a glitch when they migrated it.
Insert this rule just below RewriteEngine On line:
RewriteCond %{THE_REQUEST} /wp-content/uploads/(.+?)\.jpg [NC]
RewriteRule ^ /print/%1? [L,R=302,NE]

How to redirect a file within a folder from http to its https counterpart?

I have a website (built in ruby with .erb extensions) with mixed content (like a Wistia video) and so want to have that URL as http://domain.com.
However, when users click on "register," I want to direct them to an EV SSL-encrypted https://subdomain.domain.com/register folder.
Both of the above URLs work just fine, and the https URL displays the green EV SSL properly.
BUT, if in the low-probability event a user were to type "http://*/register" into his browser's address bar, that goes to the same /register page and allows him to register on that non-encrypted page. I really do not want that to happen.
I want to redirect anyone who tries to access the /register file via http to only the EV SSL-encrypted one, that is: https://*/register
sorry for using * wildcard, but I can only post 2 links.
I'm using Ubuntu 12.04 OS on an apache2 server and generally modify via ssh on my Mac's Terminal app.
Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^subdomain\. [NC]
RewriteRule ^register(/.*)?$ https://subdomain.domain.com/register$1 [L,NC,R=301]
Thanks for replying so quickly, Anu. That may have worked, but Rackspace support used a 301 redirect in a .conf file, and it definitely worked:
/etc/apache2/sites-enabled/domain.com.conf:5
==================================
redirect 301 /register https://subdomain.domain.com/register

.htacces redirect for AJAX search engine indexing

Ok, so I made a pure html/javascript AJAX website, but I want my pages indexable by Google.
I have my content files with meta information in plain html, but without menubar etc. and I have my index.htm with all the menubars, javascript AJAX stuff, etc.
To make AJAX indexable for google, my URL's should look like "<something>#!<somthingelse>", which Google indexbot will change to "<something>?_escaped_fragment_=<somethingelse>", such that my server knows it should return the content directly, instead of the page that loads it via AJAX.
However, since my server is stupid, as it doesn't use server side processing, I need to perform a trick via htaccess (which is where I fail :( )
The idea is as following:
I have my fancy URL's http://mysite.com/page1#!1, http://mysite.com/page2#!1, etc.
Normally, htaccess should rewrite that to /index.htm?page=page1 such that my AJAX reads the URL param and automagically loads page1.htm content file.
For Google indexer, it should ignore this rewrite for any url containing "?_escaped_fragment_=1" such that the url points to the content page directly
This way I have to make a small compromise by putting #!1 in every fancy URL, but as far as I can think of, it is the only way to do this without server side processing (except for htaccess of course)
I just cant seem to get the rewrite rules to do this.
Here's what ive come up with so far:
RewriteEngine on
RewriteCond %{QUERY_STRING} (^|.*&)_escaped_fragment_=1(&.*|$)
RewriteRule ^(.*)$ %1 [L,R=301]
RewriteRule ^(.*)$ /index.htm?page=%1 [L,R=301]

URL Rewrite for Internal (SEO) that Eliminates Original Link

My site is built in php and I have URL rewrites to make the .php links more SEO friendly:
RewriteRule ^page-one$ /pageone.php [L]
The problem with this is that someone, including Google bots, can still see pageone.php from when it was previously indexed. So I end up with two links for each page. That's bad.
How can I set this up so that I get the benefit of rewriting to page-one, while somehow making sure an external request to pageone.php is redirected to page-one?
How about redirecting page-one.php to pageone?
RewriteRule ^page-one.php$ /pageone [R=301,NC,L]

Resources