Is there a way through htaccess to block access to images from my site when requested by a specific top level domain, eg ".ru"?
I currently use:
RewriteCond %{HTTP_REFERER} ^\.ru [NC,OR]
Rewriterule ^(.*)$ https://www.google.com/images/srpr/logo4w.png [r=307,NC]
but dont think its working as intended..
Thanks!
The regular expression that you are using, ^\.ru, means "anything that STARTS with .ru", so if the referer is http://some-site.ru/some-path/some-page.html, it's obviously not going to match. Try:
RewriteCond %{HTTP_REFERER} ^https?://[^/]+\.ru/? [NC]
Related
I want the root of my website (www.bitumephotofest.it) to redirect to a subdomain (2016.bitumephotofest.it) (I am running a Wordpress multisite). It works.
But I have another subdomain (2015.bitumephotofest.it) and it also redirects to 2016.bitumephotofest.it.
I want the redirect to work only between www.bitumephotofest.it and 2016.bitumephotofest.it. 2015.bitumephotofest.it should be independent as it is a different website.
I tried to look for questions by people with a similar situation but there is always something different and, anyway, those solutions does not work for me.
Here is my code:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} 2016.bitumephotofest.it
RewriteCond %{REQUEST_URI} !wordpress/
RewriteRule ^(.*)$ /wordpress/$1 [L]
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]
RewriteRule !^(2016) http://2016.bitumephotofest.it [L,R]
Does anyone know what I am missing?
Thank you in advance!
If you want the domain 2015.bitumephotofest.it stay unmodified, you can sort of exit the rewrite rule chain with
RewriteCond %{HTTP_HOST} ^2015\.bitumephotofest\.it$
RewriteRule ^ - [L]
- as the target means don't rewrite, see RewriteRule
- (dash)
A dash indicates that no substitution should be performed (the existing path is passed through untouched). This is used when a flag (see below) needs to be applied without changing the path.
I am in the process of migrating a website from Wordpress to a new platform (Craft CMS) and I need to set up some 301 redirects via .htaccess.
Blog posts in the old website resided in the root, I need to redirect them all to a /blog/ folder (slug is the same). Of course, because they are in the root domain there's a bunch of pages there that won't be redirected—I need to exclude these (and their sub-pages) from the redirect.
How would I do this? I tried to cobble something together using information I could glean from searching but I just end up with a redirect loop:
RewriteCond %{REQUEST_URI} !^/$ [OR]
RewriteCond %{REQUEST_URI} !^/(blog|about-bluegg-creative|what-we-do|what-we-do|doodles|contact|legal-stuff|admin) [NC]
RewriteRule (.*) blog/$1 [R=301,L]
It's probably worth mentioning that there are other redirects going on in my .htaccess so the solution needs to play nicely with those. Here's a gist of the relevant parts: https://gist.github.com/hamishtaplin/c8d5e39d4621f56038d8
You don't want that [OR] flag. What that is doing is logically or of two negative matches, which means either of those will go through since you only need to negate one or the other.
You just want:
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_URI} !^/(blog|about-bluegg-creative|what-we-do|what-we-do|doodles|contact|legal-stuff|admin) [NC]
RewriteRule (.*) blog/$1 [R=301,L]
I'm trying to redirect two kind of urls to a subdomain and all the others to my main domain.
A concrete example :
"my account" pages starting by /my-account/* and subscription page must be redirected to https://my-account.domaim.com. The uri must be kept.
all others like /news or the homepage must be seen on www.domain.com only
Here is what I have tried until now :
# All urls except my-account/* and subscription are redirected to the main domain
RewriteCond %{HTTP_HOST} ^my-account\.domain\.(.+)$
RewriteCond %{REQUEST_URI} !^my-account/
RewriteCond %{REQUEST_URI} !^subscription$
RewriteRule ^(.*)$ http://www.domain.%1/$1 [L,QSA,R=301]
# subscription page is redirected to my-account subdomain
RewriteCond %{HTTP_HOST} ^www\.domain\.(.+)$
RewriteRule ^subscription$ https://my-account.domain.%1/subscription[L,QSA,R=301]
# All my-account/* pages are redirected to my-account subdomain
RewriteCond %{HTTP_HOST} ^www\.domain\.(.+)$
RewriteRule ^my-account/(.*)$ https://my-account.domain.%1/my-account/$1 [L,QSA,R=301]
Each rules work independently but if i try all of them together i'm stuck in an infinite loop.
Is anyone have an idea how to prevent it ?
The rules look fine, but you have a missing space in your 2nd rule:
RewriteRule ^subscription$ https://my-account.domain.%1/subscription[L,QSA,R=301]
# -----------------------------------------------------------------^
But you can probably combine them into a single rule:
RewriteCond %{HTTP_HOST} ^www\.domain\.(.+)$
RewriteRule ^(subscription|my-account/.*)$ https://my-account.domain.%1/$1 [L,QSA,R=301]
Also make sure you're clearing your cache when you test, as 301 redirects are permanent and the browser will cache them.
Thanks for your answer !
The typos was from my copy/paste and the combination works but doesn't change anything to the problem with the other rules. I keep it for later ;)
I tried a reversed rules like this :
#RewriteCond %{HTTP_HOST} !^my-account\.domain\.(.+)$ [NC]
#RewriteCond %{REQUEST_URI} ^/subscription$ [OR]
#RewriteCond %{REQUEST_URI} ^/my-account/ [NC]
#RewriteRule ^(.*)$ https://my-account.domain.%1/$1 [L,R=301]
It also works but not in combination with the other. it doesn't loop anymore but if i try something like http/www.domain.com/subscription i'm redirected to www.domain.com with the url truncated. It seems that the Rewrite conditions aren't correctly recognized but still can't find why ...
Let me explain the problem. We have a website in 2 languages: fr & nl (dutch).
When you arrive on our website, you land on www.domain.be which redirects
you (as you can see in the following code) to the dutch version if your browser language
is set to 'nl' and if it's set to 'en' (because dutch people often use this language for they browser) or leave you on www.domain.be / fr.domain.be (both url are work to call the website - the fr... one is more in response of the nl... one)
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP:Accept-Language} ^nl [NC]
RewriteRule ^$ http://nl.domain.be/ [L,R]
RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteRule ^$ http://nl.domain.be/ [L,R]
</IfModule>
On the website, you can choose, by clicking on 2 links (in the top right corner), if you want to go to fr.domain.be or nl.domain.be.
When you click on one of those link, the htaccess redirects you even if you want to go to the fr part (while navigating on the dutch one) and the same on the nl part.
How can I solve that? I would like the htaccess to only redirect you when you first come to the website but then be desactivated and allow the user to choose his language if he wants to.
Could you please help me? I'm on this for like two days...
You're only redirecting the site base /, so swith directly to another page should not be a problem.
Can't you just use a fake index page when you manually switch ? Like http://nl.domain.be/index
You can create multiple conditions for a rule set so you could add an aditional check that the redirect only happens when you do not match the HTTP_REFERER to your domains.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP:Accept-Language} ^nl [NC]
RewriteCond %{HTTP_REFERER} !^*\.domain\.be/ [NC]
RewriteRule ^$ http://nl.domain.be/ [L,R]
RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteCond %{HTTP_REFERER} !^*\.domain\.be/ [NC]
RewriteRule ^$ http://nl.domain.be/ [L,R]
</IfModule>
the ! means NOT so basically you are saying when the HTTP_REFERER does NOT match the url pattern, which is "(wildcard).domain.be".
This will prevent the rule from being run if they are on your site currently and trying to change the language.
NOTE: I'm not near an apache box to test this so my syntax might be off but that should get you down the right path.
I have a subdomain (store.example.com). Not only do I want to redirect traffic from store.example.com but I want to redirect it to specific files on the domain (everything I could find was based on redirecting the entire subdomain)
So I want store.example.com/Science-Fiction -> www.bundoranpress.com/category/1/Science-Fiction
How would I write this using htaccess file?
You will either need a couple of RewriteConds or (in my opinion the better approach) a php script handling your requests. For the first approach:
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} ^(ThanksForAllTheFish) [NC]
RewriteRule (.*) http://www.bundoranpress.com/$1/$2 [L,R]
This will redirect store.example.com/ThanksForAllTheFish to www.bundoranpress.com/store/ThanksForAllTheFish selling Douglas Adam's books.
Now, you could go for adding all possible terms in the second RewriteCond like so:
RewriteCond %{REQUEST_URI} ^(ThanksForAllTheFish|Universe|Hitchhiking) [NC]
This would capture one of the terms and redirect to the corresponding address (e.g. www.bundoranpress.com/store/Universe). You see that this will be difficult very quickly.
A better approach would be to redirect all urls from a specific folder and subdomain to a php script handling the rest, like so:
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} ^books/(.*) [NC]
RewriteRule (.*) http://www.bundoranpress.com/index.php?sub=$1&cat=$2 [L,R]
This would redirect store.example.com/books/ThanksForAllTheFish to www.bundoranpress.com/index.php?sub=store&cat=ThanksForAllTheFish. You could access the variables via $_GET["sub"] and $_GET["cat"] respectively. This gives you a much greater flexibility at hand.