.htaccess image to web page redirect of sorts - image

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]

Related

ReWriteCond in .htaccess not working in IE11

The purpose of this code is to prevent people from accessing a pdf file unless they are accessing the file from another page in https://www.example.com. If they are trying to access the PDF from another site, they are to be redirected to our commercial website https://example2.com. (It is to prevent people who don't have authorized access from downloading a PDF or from copying the URL for the PDF and sharing it with others.)
RewriteEngine On
RewriteCond %{HTTP_HOST}##%{HTTP_REFERER} !^([^#]*)##https?://\1/.*
RewriteRule ^(.*) https://example2.com/ [L,R=301]
The code above worked fine with all browsers until a recent update to IE11 and now it ignores the rewrite condition and always redirects to https://example2.com even if you are linking from https://www.example.com. However it works as it should in Chrome and Edge.
I tried changing the rewrite to what I have below as I am fine hardcoding the URL, but got the same results:
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^https://www.example.com/.*
RewriteRule ^(.*) https://example.com/ [L,R=301]
How do I get this needed functionality to work in IE?
Maybe IE has an old and wrong rewrite rule cached. You could clear cache in IE and try again.
Besides, you could test your rewrite rules in this site. I test with your first code and it says:
This test string is not supported: %{HTTP_HOST}##%{HTTP_REFERER}
I test with your second code and it works. So I think you could clear IE cache and try again with the second code.
For more information, you could refer to this similar thread.

How to setup a 301 redirect using mod_rewrite

I must admit, mod_rewrites are still a bit of black magic for me and I try to stay well away from them unless I really, really need to us them, now I do!
I've just migrated my site to become a multi lingual site and in doing so I've made the following change to the structure.
oldwebsite.net/somefile.php >> newwebsite.com/xx/somefile.php
oldwebsite.net/somefolder/somefile.php >> newwebsite.com/xx/somefolder/somefile.php
Now, I know how to do individual 301 redirects, but I got tons of pages and the new structure hasn't changed the page name so I would like to avoid setting up hundreds of individual 301 redirects, help!!
Thanks,
Mikael
You can put this code in your oldwebsite htaccess (which has to be in root folder)
RewriteEngine On
RewriteCond %{HTTP_HOST} oldwebsite\.net$ [NC]
RewriteRule ^(.*)$ http://newwebsite.com/xx/$1 [R=301,L]

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]

Using mod_rewrite or htaccess to apply 301's to entire site structure, with URL's built using query strings

I am taking over an old website, and need to change the domain name.
The current domain is http://www.example.com/folder/ and the new domain is http://example.school.nz
An example of an existing and desired URL is:
Before: http://www.example.com/folder/index.php?page=sport
Desired: http://example.school.nz/about-us/sport
The site is built using CMSMS (http://www.cmsmadesimple.org), so pages and URL's are generated through the CMS, although it's easy enough to export a list of URL's.
Ideally I could set up 301 redirects for the entire site so the user doesn't get 404'd. Any clues to the easiest way to accomplish this?
It depends on how general you want things to be, but this set of rules will do what you asked in your question:
RewriteCond %{HTTP_HOST} .*
RewriteCond %{QUERY_STRING} page=sport
RewriteRule ^folder/index.php http://example.school.nz/about-us/sport? [L,R=301]

How to redirect url to another

OK...
I have setup things so that when the following page is requested (browser users and servers)
http://www.visualise.ca/?_escaped_fragment_=corona
the website returns the following the content of this (HTML snapshot)
http://www.visualise.ca/corona
Where 'corona' always change, it varies depending on the page the users or servers are requesting. It could also be
http://www.visualise.ca/?_escaped_fragment_=anne-au-cherry
redirecting to
http://www.visualise.ca/anne-au-cherry
Thanks
UPDATE: OK let me be more clear. I use AJAX to load my Wordpress post and they appear like this http://www.visualise.ca/#!/corona when loaded. But it's not crawlable by Google that request to serv them as http://www.visualise.ca/?_escaped_fragment_=corona so I modified Wordpress to do so. Now Google can crawl my page and index its content and accessing the HTML snapshot available at http://www.visualise.ca/corona.
The problem is that when I paste the http://www.visualise.ca/#!/corona link to facebook it seems to read the http://www.visualise.ca/?_escaped_fragment_=corona and is unable to read the content. But when I paste directly the http://www.visualise.ca/?_escaped_fragment_=corona link it works, it reads http://www.visualise.ca/corona (The HTML).
So I thought maybe if I could redirect http://www.visualise.ca/?_escaped_fragment_=corona to http://www.visualise.ca/corona it would solve my problem.
Here is the existing .htaccess file
#--- DH-PHP handlers ---
AddHandler fastcgi-script fcg fcgi fpl
AddHandler php-fastcgi .php
Action php-fastcgi /cgi-bin/dispatch.fcgi
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
The common mistake that a lot of people do is trying to match whole URL including query string. The reality is: when matching URL, the pattern get applied to path part of it and query string has to be matched separately.
Use this rule: it will issue 301 Permanent Redirect from this kind of URL /?_escaped_fragment_=corona to /corona (where corona can be anything).
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=([^&]*)
RewriteRule .* http://%{HTTP_HOST}/%1? [R=301,L]
I guess your question is really "how do I do this kind of redirection?". So the answer is here:
There are three ways that I could think of, each with slight differences.
Doing the redirection server side
This basically means you set redirect headers in your response. In php, you could do this using the header function. It can also do a delayed redirect, in which case you need to worry about the contents of the page.
Doing it using client side using html's "http-equiv" meta tags. This way the page always gets loaded. Example here.
doing it via javascript. Thats you basic document.location.href thing. You need to figure out a way to pass the argument to javascript, or have your JS read it from the address url itself.
Since I've shown you 3 ways of doing this, I really hope that's what you're looking for :P
Update after seeing the comments:
The above methods will cause the URL to change. If you don't want the URL to change, but show the contents of that other page on your original page, you caould either do that using iframes (baaaad), or do the decent thing and set up URL rewriting.
:)
RewriteEngine on
RewriteRule \?_escaped_fragment_=(.*?) /$1 [L,R=301]
is it what are you looking for?

Resources