.htaccess re-write works but I want to block domain from displaying origin. IPTables - apache

I have setup the .htaccess file to rewrite anyone unwanted domains pointing to my VPS - There is only one domain left that gets mod-rewritten. I want to completly block this Domain from even accessing my origin IP address so I need help with the IP Tables - and how to stop targetdomain.com linking to my site!
Can anyone tell me what needs to be done on the Origin Server to deny access and forwarding from the target domain!

You can't stop someone else from having a DNS entry pointing to your server, but you could create a VirtualHost for that domain name, and configure that VirtualHost to always serve an error page for any URL. You mentioned iptables, but iptables doesn't deal with domain names -- you won't be able to use it to block a domain name.
Here's a very simple VirtualHost configuration you might put in your Apache config files:
<VirtualHost *:80>
DocumentRoot "/path/to/some/folder"
# You can only have one ServerName which is the main domain that
# you want to block, but if you have additional domains to block,
# you can add them with ServerAlias directives.
ServerName baddomain.com
ServerAlias another.baddomain.com
# This redirects all requests on this domain to index.html, so
# that visitors will see your message no matter what path they
# go to.
RewriteEngine On
RewriteCond %{REQUEST_URI} !=/index.html
RewriteRule ^ /index.html [R=302]
</VirtualHost>
Now, inside the folder /path/to/some/folder (which you should replace with an actual folder path on your server), just create an index.html file with whatever message you want to show to anyone who visits this domain.
Example:
<!doctype html>
<html>
<head>
<title>Blocked domain</title>
</head>
<body>
<h1>Blocked domain</h1>
<p>The domain name you tried to connect to is blocked.</p>
</body>
</html>
Alternatively, you could have it redirect them to another website or whatever else -- read up on mod_alias or mod_rewrite for info on how to do this.

Related

Redirect http to https on apache not working for http links that include a filename

I have recently setup SSL on my apache server (the server is hosted on DigitalOcean). I have followed the instructions to setup the certificate and edit to the server conf file to redirect http to https. Everything works file but I have a problem when using an http url that includes a file. In that case, the file is appended to the domain name without the / so the browser shows a File not found message (which is correct).
http://www.my-website.com redirects correctly to https://www.my-website.com
however,
http://www.my-website.com/file1.html redirects to https://www.my-website.comfile1.html (the / after the server name is missing)
Can someone tell me what the problem is? The redirection commands in the apache .conf file are as follows:
<VirtualHost *:80>
ServerName my-website.com
Redirect permanent / https://www.my-website.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
</VirtualHost>
Thanks
Following the link suggested in the comment by Don't Panic, I tried the solution from the original Apache documentation, http://httpd.apache.org/docs/current/rewrite/avoid.html#redirect, and it worked. I am not sure if the problem was the lack of the quotes in the redirect statement (I previously tried the exact same thing without the quotes and it did not work). The solution that worked for me is the following:
<VirtualHost *:80>
ServerName www.example.com
Redirect "/" "https://www.example.com/"
</VirtualHost>
The syntax for the Redirect directive from the Apache documentation is:
Redirect [status] [URL-path] URL
The issue in this case is that Redirect appends any remaining path after the URL-path parameter to the URL.
Hence in "http://www.my-website.com/file1.html", the remainder is "file1.html", which will be appended directly to the given URL, resulting in "https://www.my-website.comfile1.html".
The solution is to add a slash at the end of the URL, changing the line to:
Redirect permanent "/" "https://www.my-website.com/"
(Putting both parameters in quotes is also helpful, but not the cause of the issue).
Also, note that if the URL-path parameter was not "/", a trailing slash would not be needed on the URL, in effect the URL-path and URL need to match. For example:
Redirect permanent "/one" "https://www.my-website.com/two"

Apache: Redirect requests where ServerName doesn't match

I'm relatively new to Apache rewrite rules. What I need to do I think should be relatively easy, but I could use a bit of help.
I have a number of name based virtual hosts defined in my Apache configs, for developers to test new feature branches, and I have a wildcard DNS CNAME setup to direct traffic.
Everything works as it should when a request matches the ServerName in one of the virtual hosts. However if the hostname in the request doesn't match any explicitly defined virtual hosts, it automatically uses the first virtual host.
This is quite confusing, as a developer may think they're accessing the correct virtual host, when in fact they're not.
What I'd like to do, is define a rewrite rule in the first virtual host, so that if the hostname in the request doesn't exactly match the defined "ServerName", it will redirect it.
For example...
If I have 2 virtual hosts defined like so...
<VirtualHost *:443>
ServerName default.mydomain.com
...
</VirtualHost>
<VirtualHost *:443>
ServerName my-feature-1.mydomain.com
...
</VirtualHost>
and a developer is trying to request the site for their new feature, but spell it incorrectly, i.e.
https://feature-1.mydomain.com
The first virtualhost will silently serve the request, and they may be none the wiser, and wonder why their new feature code appears not to be working.
What I would like it to do instead. I'd like to redirect them to...
https://default.mydomain.com
so that it's obvious they've misstyped the URL.
I've got it to work with the following rewrite rule in the first virtual host...
RewriteCond %{HTTP_HOST} "!^default\.mydomain\.com" [NC]
RewriteRule ^/(.*) https://default.mydomain.com [L,NE,R=301]
This works as the default for any request not explicitly matched by the subsequent dynamic virtual hosts but also redirects to the preferred default URL. I don't want to actually serve any content on undefined URL's.
The only thing that would improve it slightly would be to not have to duplicate the domain name in the rules.

VirtualHosts fallback to rewriting URL

I'm setting up an Apache web server, and I have a bunch of VirtualHost entries in my httpd.conf, which all work fine. All subdomain requests on my domain (eg. test.mydomain.com, test2.mydomain.com) are sent to the same IP address (same server). I'd like to rewrite any unrecognized subdomains to simply www.mydomain.com or just mydomain.com if there isn't a VirtualHost entry for them. Is this achievable using Apache?
Thanks.
You could set a Redirect rule in your default VirtualHost (ie, the one that's used when accessing the server via a host without a VirtualHost):
Redirect permanent / http://example.com/
If that doesn't suit your needs (like if you want to still be able to access some files on the fallback DocumentRoot), you could set a PHP script as your 404 handler:
httpd.conf
ErrorDocument 404 /404.php
404.php
<?php
header($_SERVER["SERVER_PROTOCOL"] . " 301 Moved Permanently");
header("Location: http://example.com");

How to Route Various domain Aliases to Fetch their Own Folders with Own Webpage PHP Files?

imagine the root of a server where multiple aliases such as website.nl; website.de; etc. all direct to the root the root \httpdocs\ with this physical hosting:
httpdocs\...
httpdocs\holland\ # webpages in Dutch (home.php | contact.php | etc)
httpdocs\deutsch\ # webpages in English (home.php | contact.php | etc)
httpdocs\images\ # all multilingual webpages share the same images
httpdocs\js_css\ # all multilingual webpages share the same scripts/layout
httpdocs\.htaccess # here be a clean root, nothing else than .htaccess
Thus, only the webpages .php differ: the rest they all share the same! Now imagine that you want to configure .htacces via apache script to make "bridge the gap" if you will, between the root and the folder, making it possible to type this in browser and below water fetching the right php webpage, but keeping the shorter url in the browser:
website.nl/home.php
//files fetched should come from the holland folder associated with website.nl
website.de/home.php
//files fetched should come from the deutsch folder associated with website.de
(As opposed to seeing this in the browser: website.nl/holland/home.php | website.de/deutsch/home.php)
What apache script line will do such thing?
Thanks: Much appreciated!
You need to have "mod_rewrite" installed in apache.
Assuming you do, place a .htaccess file in the root directory of that account
(probably:)
~/public_html/.htaccess
and then put this in that file
RewriteEngine on
RewriteCond %{HTTP_HOST} ^website.nl(.*)$ [OR]
RewriteCond %{HTTP_HOST} ^www.website.nl(.*)$
RewriteRule ^.*$ "http\:\/\/www\.website\.nl\/holland\/$1" [L,P]
And then repeat the rewrite condition for website.de
Basically that says, grab the section after the website.nl part, and paste it on the part after holland, but the "L,P" says to make that a silent redirect and keep the user on the same url as they entered with.
goodluck!
Assuming you have full control over the server configuration, I would tend to a setup without mod_rewrite. Instead I'd use two separate VirtualHosts for each country site, and Alias directives to map the shared directories. In an abridged version, something like this:
<VirtualHost website.nl>
DocumentRoot /httpdocs/holland
ServerName website.nl
Alias /images /httpdocs/images
Alias /js_css /httpdocs/js_css
</VirtualHost>
<VirtualHost website.de>
DocumentRoot /httpdocs/deutsch
ServerName website.de
Alias /images /httpdocs/images
Alias /js_css /httpdocs/js_css
</VirtualHost>
this lets you work freely in both languages, and add new resources and URLs without having to edit anything - only if a shared resource is added, you have to touch the Alias directives and restart the server.

Apache: Redirect blog.foobar.com to www.foobar.com

I have a site at blog.foobar.com that I have closed down, and I want any
page requested there to be forwarded to www.foobar.com
I want my VirtualHost config to do this for me. I currently have the following lines that does nearly what I want but not exactly:
redirect permanent / http://www.foobar.com
Unfortunately what happens is that if I ask for blog.foobar.com instead of forwarding to www.foobar.com it serves the pages on blog.foobar.com instead.
Is there a way doing this in the VirtualHost config or should I use a .htaccess file instead?
Regards
Steve
You can use the Redirect directive in the context of either a VirtualHost or a .htaccess file. However, what you probably want is a RedirectMatch:
RedirectMatch permanent (.*)$ http://www.foobar.com$1
With that inside your blog.foobar.com VirtualHost, any request to blog.foobar.com would be directed to the same page on www.foobar.com, ie. blog.foobar.com/my/page would go to www.foobar.com/my/page.