skip to Main Content

Situation: I have a single (main) domain, which has several aliased domains, each of which are pointing at the same Plesk-based server (for instance, I have example.com as main, with something.net, anotherone.co.uk, and several others all as aliases of the main domain account). This means that whenever I enter the domain name into my address bar of any of the aliases, it goes directly to the account of the main domain (example.com).

Problem: Based on the domain name of the alias(es), I have an index.php that redirects each domain differently (for instance, requests to domain A redirects to a corporate site, domain b goes to a thanks site etc.) Which works great, but if a directory is added after the domain URL (i.e. somealias.com/something) then it gives a 404 not found error.

What I would really appreciate, if someone can help me out, is a (single if possible) rewrite ruleset that would essentially strip off ALL trailing directories and/or GET requests, and only leave the typed-in base URL, so then the php script sitting in the main domain document root can take over and deal with the request appropriately.

Strangely enough, I’ve not been able to find a (simple) solution for this anywhere. Is it a case of having to define a rule for each of the aliased domains individually?

3

Answers


  1. Try the following,

    #Options +FollowSymLinks
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www.olddomain.com$[OR]
    RewriteCond %{HTTP_HOST} ^olddomain.com$
    RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L] 
    
    Login or Signup to reply.
  2. RewriteEngine On
    RewriteBase /
    
    RewriteCond %{REQUEST_URI} !=/
    RewriteCond %{REQUEST_URI} !=/index.php
    RewriteRule .* http://%{HTTP_HOST}/? [L]
    

    This will take ALL requests except root folder / (e.g. http://example.com/) or index file (e.g. http://example.com/index.php) and redirect them to the root folder (e.g. http://example.com/some-url will be redirected to http://example.com/).

    You may need to replace index.php by the file that is get executed when you hit the root folder (Apache will silently rewrite http://example.com/ to http://example.com/index.php (depending on your actual settings) as it needs to have a file to execute otherwise it may show an error).


    Alternatively (possibly even better — depends on your actual setup and requirements) you may use these rules — this will redirect only non-existing URLs. So if you have an image meow.png on your site, these rules will allow you to access it (http://example.com/meow.png):

    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule .* http://%{HTTP_HOST}/? [L]
    

    UPDATE:
    If you going to place this into config file (httpd-vhost.conf or httpd.conf) then use these rules:

    RewriteEngine On
    
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
    RewriteCond %{REQUEST_URI} !=/index.php
    RewriteRule .* http://%{HTTP_HOST}/? [L]
    
    Login or Signup to reply.
  3. It seems to me that all the sites are hosted on the same server (probably using the same code base).
    If your index.php is a front controller you can redirect everything to your index.php and decide in the first lines of index.php what front controller to load (like backend.php).
    If you don’t mind having to maintain a list of the aliases you can define a hash of [alias] => path-to-front-controller.
    In the front controller of you main domain you check the alias name (using $_SERVER[‘SERVER_NAME’] for example) against the hash and load the appropriate file.
    You will have to add and entry to the hash each time you add anew alias. If they are not generated dynamically maintaining this hash is not a lot of hassle.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search