10

I'm making an advertisement blocking DNS server that redirects all the advertisement domains to an instance of Apache. So something like

www.addomain.com

gets redirected to

dnsserver.localnetwork

However, occasionally I come across links more like

www.addomain.com/some_ad

which gets redirected to

dnsserver.localnetwork/some_ad

which doesn't even exist and hence raises a 404 error.

I was hoping that there would be some way to make Apache serve index.html, regardless of the actual request. Otherwise, if you have an alternate solution that does not involve Apache, I'm up for that too.

4
  • 1
    You could have a look at apaches mod_rewrite. Redirecting everything from after the domain to /index.html Jun 18, 2014 at 12:02
  • This seems like an inefficient way of doing adblocking.
    – Panther
    Jun 18, 2014 at 15:23
  • @bodhi.zazen How would you do it? Most adblockers work on a similar concept. Block or redirect the requests to the advertising domains. I'll be glad to implement a better alternative if you have one. Jun 18, 2014 at 15:43
  • 1
    To be honest, I use noscript and the default adblock plugins. If that is not sufficient, then you can either use a hosts file (takes work to maintain) or a proxy (privoxy), but again it takes more work to maintain a blacklist yourself. See addons.mozilla.org/en-US/firefox/addon/noscript and addons.mozilla.org/en-US/firefox/addon/adblock-plus , you may need alternates depending on your browser.
    – Panther
    Jun 18, 2014 at 15:50

3 Answers 3

20

In Ubuntu, make sure mod_alias is enabled:

sudo a2enmod alias

Then in your VirtualHost directive you can use AliasMatch.

Example:

<VirtualHost *:80>
    DocumentRoot /path/to/your/host
    ServerName yourdomain.com
    DirectoryIndex index.html

    AliasMatch ^/(.*)$ /path/to/your/host/index.html

    <Directory "/path/to/your/host">
      Require all granted
    </Directory>
</VirtualHost>

Now everything gets redirected to /index.html.

2
  • My JS, CSS, and images are getting redirected as well with this solution. Is there any way to prevent this without doing a big regex that checks the extension of the request?
    – Ameo
    May 11, 2018 at 8:46
  • You could always structure your project so that JS/CSS and images are in separate folders from your index.html and then route different URLs to them, e.g. coming from django, throw them all in a folder called static, your index.html in a folder called templates, then route all URLs with a <YOUR_HOST>/static/... to the static folder and everything else to the index.html in the template folder. Nov 29, 2020 at 8:52
8

You need a rewrite rule in your .htaccess file. Something like the following should work:

RewriteRule ^*$  http://www.addomain.com/index.html [R=301,NC,L]
1
  • 2
    That does a redirect. It doesn't directly serve index.html.
    – slang
    Sep 26, 2018 at 2:08
1

Another more simple option would be to change the 404 error page to the static page that you want to show for all pages on your site. So, look for this line (or something similar) in your apache2 configuration files:

ErrorDocument 404 /errors/not_found.html

And change the "/errors/not_found.html" to: "/index.html"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .