How to block requests from domain other than mine? - apache

I have a wordpress website say mydomain.in, Someone is using domain masking and able to open my website on their domain(lets say external.com) without my permission. I have this .htaccess rules but its not working.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_REFERER} !mydomain.in
RewriteRule ^.*$ - [F]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule .* https://www.mydomain.in/? [R=301,L]
</IfModule>
But it still opens from their domain. What is the solution for this

Have it like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(?:www\.)?mydomain\.in$ [NC]
RewriteRule ^ - [F]
RewriteRule ^index\.php$ - [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
Take note of this new rule:
RewriteCond %{HTTP_HOST} !^(?:www\.)?mydomain\.in$ [NC]
RewriteRule ^ - [F]
That checks if HTTP_HOST of a request is not mydomain.in then simply block the request with 403 - Forbidden status.

Related

.htaccesss: allow to load resource files

I am trying to do the following.
denied directly access to example.domain.com
allow access from to example.domain.com from another.domain.com
allow to load resource files (css, js) when the the user comes form another.domain.com to example.domain.com
Points 1 and 2 i solved like this:
RewriteCond %{HTTP_REFERER} !http(s)?:\/\/(www\.)?another\.domain\.com\/$ [NC] RewriteRule ^.*$ - [F]
But i can not to load resource files. I get the error message 403.
I tried with this code, but it's not working.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 ^(index\.html|robots\.txt)
RewriteRule ^.*$ ^.*$ [L]
RewriteCond %{HTTP_REFERER} !http(s)?:\/\/(www\.)?another\.domain\.com\/$ [NC]
RewriteRule ^.*$ - [F]
I am a quite new with htaccess, can someone help me to solve this problem?
With your shown samples, please try following .htaccess rules file. Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^(www\.)?example\.domain\.com$ [NC]
RewriteRule ^ - [F,L]
RewriteCond %{HTTP_HOST} ^(www\.)?another\.domain\.com$ [NC]
RewriteRule ^(.*)/?$ http://example.domain.com/%1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} (?:index\.html|robots\.txt)/?$ [NC]
RewriteRule ^ - [F,L]
i have found a solution for me:
RewriteCond %{REQUEST_FILENAME} ^.+(\.js|\.css|\.json|\.jpg|\.png|\.woff2|\.woff|\.ttf)$
RewriteRule ^.*$ - [L]
RewriteCond %{HTTP_REFERER} !http(s)?:\/\/(www\.)?another\.domain\.com\/$ [NC
RewriteRule ^.*$ - [F]

How can I redirect all www traffic to non www url in vue project

I have the following code in my htaccess file which is supposed to redirect all www requests to the non www version of index.html, but its not working and I'm not sure why. Can anyone help?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
Have it like this:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
RewriteRule ^index\.html$ - [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.html [L]
Here we are capturing value after www. in a capture group and then using that as %1 in target URL.
Make sure to clear your browser cache or use a new browser before you test this change.

Non-www RewriteRule not working on request URI

I need a website to have all URLs NOT have www in front of it. For this, I have found some useful bits of code here that have helped me out a lot. However, nowhere can I find how to properly do it for ALL request URIs. Currently it only works on the domains homepage.
# 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]
RewriteCond %{HTTP_HOST} ^www\.domain\.nl [NC]
RewriteRule ^(.*)$ https://domain.nl%{REQUEST_URI} [L,R=301]
</IfModule>
# END WordPress
Right now www.domain.nl gets rewritten into domain.nl. However, any other like www.domain.nl/anything_else do not get rewritten (also tried adding a / before the %{REQUEST_URI}, did not work).
Any help is appreciated!
With www rule before:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.domain\.nl [NC]
RewriteRule ^ https://domain.nl%{REQUEST_URI} [NE,L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Multiple Re-Write Conditions

Is there a way I should be writing multiple re-write conditions for my domain? I want the I.P to forward to the domain name and all domain name variations forwarded to https. The domain can be found at https://www.getrideshare.com. I've included the snippet below in the .htaccess file.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^107\.180\.51\.9
RewriteRule (.*) https://getrideshare.com/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
You can do your rule this way.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^107\.180\.51\.9 [OR]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://getrideshare.com/$1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Let me know how it works out.

Redirecting HTTP to HTTPS correctly in Question2Answer script

I have searched for this question for many days and tried so many different methods but nothing works so far. I am using the Question2Answer script and I want to redirect all the HTTP requests to HTTPS.
My URL structure is set to :
/123/why-do-birds-sing (requires htaccess file)
and the htaccess file is as follow:
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php?qa-rewrite=$0&%{QUERY_STRING} [L]
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
</IfModule>
This correctly redirects http://example.com to https://example.com. However, if the user enters an address like this : www.example.com/users they are redirected to https://example.com/index.php?qa-rewrite=users which returns a 404 error.
The index.php?qa-rewrite= is added automatically and removing it from the htaccess totally messes up everything and I think it should be there.
This is because you need all of your redirect rules before any of the routing rules (the ones without the R flag), so:
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php?qa-rewrite=$0&%{QUERY_STRING} [L]
</IfModule>
I use this:
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^(.*)$ "https\:\/\/example\.com\/$1" [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php?qa-rewrite=$0&%{QUERY_STRING} [L]
</IfModule>
If you don't like to redirect www, you can remove the line
RewriteCond %{HTTP_HOST} ^www\.example\.com$ and the [OR] above