.htaccess redirection for image link - .htaccess

I would like to know if it is possible to set up a redirect in the .htaccess file so that if another website directly links to an image on my site, instead of the image opening in a browser window on its own, the page that the image is hosted on is displayed.
This page has the same name as the image i.e. if the image the other site is linking to is
Case1
www.example.com/subdirABC/gallery/image.jpg
**to**
www.example.com/subdirABC/
**or**
www.example.com/subdirABC/index.php
Case2
www.example.com/subdirXYZ/gallery/image.jpg
**to**
www.example.com/subdirXYZ/
**or**
www.example.com/subdirXYZ/index.php

Its work for me
RewriteBase /
Options +FollowSymLinks
RewriteCond %{HTTP_REFERER} !^https://example.com
RewriteRule ^(.*)/gallery/(.*.jpg) /$1/ [L,R=301]

Try something like the following in .htaccess:
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^https?://(www\.)?example\.com
RewriteRule ^([^/]+/)gallery/[\w-]\.jpg$ /$1 [R,L]
What this says is... for all requests that match the pattern /<subdir>/gallery/<filename>.jpg and are not being linked to from your site (example.com) then redirect to /subdir/.
Additional notes...
Direct links (which includes search engine bots) and user-agents that fail to send the HTTP Referer header will also be redirected to the "page".
<filename> contains just the characters a-z, A-Z, 0-9, _ and -.
$1 is a backreference to the first captured group in the RewriteRule pattern, ie. ([^/]+/) - which matches subdirXYZ/ in your example.
This is a 302 (temporary) redirect.

Related

How can I redirect subdomain.domain.com/* to domain.com/folder/*

I have to make a redirection on a .htaccess file.
I want to access the content of a folder from a subdomain url like this:
subdomain.domain.com/* => domain.com/folder/*
The folder contains pdf files and I want to acces it with this url for example:
subdomain.domain.com/file.pdf
I'm new into htaccess redirection rules and I'm a little lost.
I tried something like this and test it into https://htaccess.madewithlove.com/
RewriteCond %{HTTP_HOST} ^subdomain.domain.com/*$
RewriteRule ^(.*) https://domain/folder/ [L,R=301]
This code works on the tester but on my website it throws me the error : "The connection was reset".
Do you have any idea on it?
UPDATE
Following some advices I try but it doesn't work
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com/
RewriteRule (.+\.pdf)$ https://example.com/folder/$1 [R=302,L]
RewriteCond %{HTTP_HOST} ^subdomain.domain.com/*$
RewriteRule ^(.*) https://domain/folder/ [L,R=301]
You are not doing anything with the captured URL-path (ie. (.*)) so this will always redirect to https://domain/folder/ (no file).
I would also question whether this should be a 301 (permanent) redirect. Maybe a 302 (temporary) redirect would be preferable here? Note that the 301 is cached, so you will need to clear your browser cache before testing.
It should be like this:
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com
RewriteRule (.*) https://domain/folder/$1 [R=302,L]
The $1 backreference contains the URL-path captured in the RewriteRule pattern.
The Host header (ie. the value of the HTTP_HOST server variable) contains the hostname only. There is no URL-path component. (Fortunately /* matches the slash 0 or more times, so it still "worked" in your testing.)
However, if the folder only contains .pdf files then you should be more restrictive and redirect only .pdf requests. For example:
:
RewriteRule (.+\.pdf)$ https://domain/folder/$1 [R=302,L]

Redirect Each Page of Old Domain to Same Page URL on the New Domain

I know this question has been asked with all variations but I still cannot find the exact answer. I have changed my domain name. All website files and urls are still the same. old domain still points so the same server and I have the following code in htaccess file.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
RewriteRule ^(.*)$ "https\:\/\/www\.newdomain\.com\/$1" [R=301,L]
Problem is that this only redirects the old domain home page. What can I do so I don't have to add individual redirects for each page.
It sounds like you've put these directives in the wrong place in your .htaccess file. They need to go at the beginning of the file, not at the end.
These directives look typical of cPanel generated redirects. cPanel always places redirects at the end of the .htaccess file (which is often incorrect).
If you have a front-controller before this (such as that used by WordPress) then you will find that these directives will only get processed for requests to the document root (ie. the "homepage"), as all other requests will be stopped by the front-controller.
These directives can also be simplified:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com [NC]
RewriteRule (.*) https://www.newdomain.com/$1 [R=301,L]
I've removed the $ from the end of the CondPattern so as to match a FQDN (that ends in a dot).
There is also no need to backslash escape colons, slashes and dots in the RewriteRule substitution, as these characters carry no special meaning here (it's not a regex). (This unnecessary escaping is very typical of cPanel.)

Redirect certain subfolders by removing the parameter question mark

I am using .htaccess to redirect certain subfolders of my domain, to remove the question mark to improve my URLs.
Currently my URLs are like this:
www.example.com/post/?sometitle
I am trying to remove the question mark, so it is the following URL:
www.example.com/post/sometitle
Currently I have the following code in my .htaccess file:
RewriteCond %{THE_REQUEST} /post/?([^\s&]+) [NC]
RewriteRule ^ /post/%1 [R=302,L,NE]
i am using php GET parameters, i am attempting for when the browser visits example.com/post/sometitle that the page that is currently example.com/post/?sometitle is displayed
In that case you need to the opposite of what you are asking in your question: you need to internally rewrite (not externally "redirect") the request from example.com/post/sometitle to example.com/post/?sometitle.
However, you must have already changed all the URLs in your application to use the new URL format (without the query string). You shouldn't be using .htaccess alone for this.
I also assume that /post is a physical directory and that you are really serving index.php in that directory (mod_dir is issuing an internal subrequest to this file). So, instead of /post/?sometitle, it's really /post/index.php?sometitle?
For example:
RewriteEngine On
# Rewrite /post/sometitle to filesystem path
RewriteRule ^post/([\w-]+)$ /post/index.php?$1 [L]
So, now when you request /post/sometitle the request is internally rewritten and handled by /post/index.php?sometitle instead.
I have assumed that "sometitle" can consist of 1 or more of the characters a-z, A-Z, 0-9, _ and -. Hence the regex [\w-]+.
If this is a new site then you can stop there. However, if you are changing an existing URL structure that has already been indexed by search engines and linked to by external third parties then you'll need to redirect the old URLs to the new. (Just to reiterate, you must have already changed the URL in your application, otherwise users will experience repeated redirects as they navigate your site.)
To implement the redirect, you can add something like the following before the above rewrite:
# Redirect any "stray" requests to the old URL
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ([\w-]+)
RewriteRule ^post/$ /post/%1 [R=302,NE,QSD,L]
The check against the REDIRECT_STATUS environment variable is to ensure we only redirect "direct requests" and thus avoiding a redirect loop.
(Change to 301 only when tested as OK, to avoid caching issues.)
In Summary:
RewriteEngine On
# Redirect any "stray" requests to the old URL
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ([\w-]+)
RewriteRule ^post/$ /post/%1 [R=302,NE,QSD,L]
# Rewrite /post/sometitle to filesystem path
RewriteRule ^post/([\w-]+)$ /post/index.php?$1 [L]
UPDATE: If you have multiple URLs ("folders") that all follow the same pattern, such as /post/<title>, /home/<title> and /build/<title> then you can modify the above to cater for all three, for example:
# Redirect any "stray" requests to the old URL
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ([\w-]+)
RewriteRule ^(post|home|build)/$ /$1/%1 [R=302,NE,QSD,L]
# Rewrite /post/sometitle to filesystem path
RewriteRule ^(post|home|build)/([\w-]+)$ /$1/index.php?$2 [L]
Aside: (With my Webmasters hat on...) This is not really much of an "improvement" to the URL structure. If this is an established website with many backlinks and good SE ranking then you should think twice about making this change as you could see a dip in rankings at least initially.
If only changing from query is your requirement then try with below, we are using QSD flag to discard our query string after our rule matched.
RewriteCond %{QUERY_STRING} ([^\s&]+) [NC]
RewriteRule ^ /post/%1 [R=302,L,NE,QSD]

Pretty url doesn't show in url-bar

I have an htaccess file with several redirects, Now I want to create a pretty url for some link. I tried the following sentence and it does nothing:
RewriteEngine On
RewriteRule ^/example1.html /?page_id=100 [NC]
When I type www.MyDomain.com/?page_id=100 in my browser, the site shows, but the url still looks the same as I typed it. How would I change my sentence to show example.html in the url-bar instead of ?page_id=100 ?
Thanks in advance
You have a rule that says:
When the browser requests: /example1.html
Then show them: /?page_id=100
It goes one way, it doesn't do anything if the browser requests /?page_id=100. If you want to do something about a browser requesting the query string URL, *you need a rule to tell mod_rewrite to do it*:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(index\.php)?\?page_id=100($|\ |&)&?([^\ ]*)
RewriteRule ^ /example1.html?%3 [L,R=301]
Also, you may want to get rid of the / in the pattern of your rule:
RewriteRule ^example1.html /?page_id=100 [NC]
Leading slashes are stripped off when matching against rules in an htaccess file.

.htaccess 301 redirect path and all child-paths

I want accesses to e.g. www.thisdomain.com/docs/path1/path2 to redirect to www.thatdomain.com/path1/path2
(Note that docs is not a part of the new path)
I have the following on www.thisdomain.com:
RewriteEngine on
RewriteRule ^docs/* http://www.domain.com/ [R=301,L]
If I access www.thisdomain.com/docs, it directs to www.thatdomain.com, but if I access a child-path like www.thisdomain.com/docs/path1/path2 it fails. Is it possible for the redirect to intercept the child-path access and redirect as I need? If so, any pointers?
Thanks.
With regular expressions, * means any number of the previous atom, which will match /docs and /docs/. Try this:
RewriteEngine on
RewriteRule ^docs$ http://www.domain.com/ [R=301,L,QSA]
RewriteRule ^docs/(.*) http://www.domain.com/$1 [R=301,L,QSA]
(QSA is query string append, so /docs/foo?bar=baz won't lose the ?bar=baz.)
According to section "Redirect Old domain to New domain using htaccess redirect" of the first Google result which I found searching for "htaccess redirect" (without the double quotes), this piece of code will suffice:
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
According to their description of the technique, it will redirect the directory the .htaccess file is placed in recursively (including any child paths), just as you intend. Of course, mod_rewrite needs to be present for the rewrite directives to work.

Resources