Skip to main content

Domain and subdomain pointing

Stop here!  If you want to do domain pointing, before you even start with htaccess code, make sure your pointed domain name resolves to your main domain name default page.  Many people think they have trouble with their htaccess code only to find the domain name is not properly pointed to their package in the first place.  So make sure your pointed domain name works, resolving to your default page, before going any further.  This is obviously not an issue if you are doing subdomain pointing.

The examples below assume that your root public directory (where you would put this .htaccess file) is htdocs, which is the default setup for Apache.   If your setup is different, then you may need to adjust the examples accordingly.

This approach has you code the first part of the domain or subdomain name and the associated subdirectory in your .htaccess file.   For example:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/
subdirectory/
RewriteCond %{HTTP_HOST} ^(www\.)?
name\.
RewriteRule ^(.*)$ 
subdirectory/$1 [L]

Note the second line has name, NOT name.com.   And note that there are some restrictions when using this method.
  • Domains and subdomains are treated exactly the same.  The pointing is based on the first argument of the url.  In this example name.com (as a pointed domain name) or name.domain.com (as a pointed subdomain) ends up at htdocs/subdirectory.   This is particularly useful in certain situations, for example if you want the subdomains of more than one domain to point to the same place, or if you have both a .com and a .net domain name you want pointed to the same place.
  • You must create a subdirectory under htdocs for every domain and subdomain you wish to be pointed.  The subdirectory name is then coded into the htaccess file.
  • The first argument in the url must be unique.  You cannot have two domains or subdomains with the same name.  sub.domain1.com and sub.domain2.com will both go to the same subdirectory.
  • It defaults to allow www. as a prefix on any domain or subdomain.
  • The htdocs directory is the "drop through" for any domain or subdomain not found.  You may want to put an error not found page as the default page.  Or, you could choose to have your main website be the "drop through" and just leave that one in htdocs.
There is also a simplified variation of this method that can be used in less complex situations where you are simply pointing a domain name and want ALL subdomains of that one domain to be pointed to a specific directory.   In this example name.com and any subdomain (including www) of that domain would be pointed to htdocs/subdirectory:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/
subdirectory/
RewriteCond %{HTTP_HOST} 
name.com$
RewriteRule ^(.*)$ 
subdirectory/$1 [L]

In both examples, the htacess code works this way.   The first two lines set up the Apache server rewrite process.   Then you have two RewriteCond statements and a RewriteRule for EACH domain or subdomain you wish to point.

There is a second approach where pointed domains and subdomains become automatic, meaning no specific code is required for each pointer.   While this method is much easier to use and does not need to be modified when adding more domains or subdomains, note that there are some restrictions when using this method.
  • Domains and subdomains are treated exactly the same.  The pointing is based on the first argument of the url (except www if used).
  • You must create a subdirectory under htdocs with a - at the front for every domain and subdomain you wish to be pointed.  The directory name must match the first argument in the url (except for www if used).  For example, domain.com would have htdocs/-domain and sub.domain.com would have htdocs/-sub and domain2.com would have htdocs/-domain2  etc.
  • The first argument in the url, and therefore the directory name, must be unique.  You cannot have two subdomains with the same name.  sub.domain1.com and sub.domain2.com will both go to the directory htdocs/-sub
  • You cannot have any other directory names that start with a - character.
  • It defaults to allow www. as a prefix on any domain or subdomain.
  • The htdocs directory is the "drop through" for any domain or subdomain not found.  You may want to put an error not found page as the default page.  Or, you could choose to have your main website be the "drop through" and just leave that one in htdocs.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/-
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]*)
RewriteCond %{DOCUMENT_ROOT}/-%2 -d
RewriteRule ^(.*)$ -%2/$1 [L]

The htaccess code works this way.  The first two lines set up the Apache server rewrite process.  The first RewriteCond allows www. to be a prefix and captures the request name.  The second RewriteCond checks for the existence of a subdirectory with the request name.  The third RewriteCond ensures we are not caught in a looping operation.  The RewriteRule rewrites the request with the subdirectory name (which was already verified).

Forcing or eliminating the WWW.
If you want your website to always use the WWW. prefix (like www.domain.com) then this htaccess code is your answer.   You may want to force the WWW becuaase you like it that way, or because you want consistency for search engines.   What ever the reason, use this htaccess code and you will always have the WWW domain prefix used for your website.

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]


The way this works is if the domain name does not start with WWW then the request is rewritten with the WWW.   The WWW is really just treated as any other subdomain.   So if you have other subdomains (like sub.domain.com) where you do not want the WWW added (converted to www.sub.domain.com), then you need to include another RewriteCond line for each one.

Or if you want to always eliminate the www prefix, use this htaccess code.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]


Fixing the trailing slash problem
If you have a pointed domain or subdomain, and you which to load the default page for a subdirectory under the pointed directory, and you do not have a trailing slash on the address, you get the pointed directory name inserted in the URL.   Use this htaccess code to fix that problem

RewriteRule ^([^\.]+[^/])$ http://%{HTTP_HOST}/$1/ [R=301,L]

The way this works is if the request does not have a slash at the end and does not contain a period then it will be rewritten with a slash at the end.   So you cannot use this method if your directory names contain periods or if you use search arguments in the defualt loading page without specifying the page name (with a period in it).

Be sure and put these two lines above the other redirects in the .htaccess file, but after the RewriteEngine On statement.

Comments

Popular posts from this blog

21 "teure" Arbeitsplätze in der Zukunft, vielversprechend und nur schwer durch künstliche Intelligenz zu ersetzen

Bei der Suche nach einem Arbeitsplatz sind die beiden Top-Belange, die Sie oft in Betracht ziehen,: Gehalt und Aussichten für die Zukunft. Vor kurzem hat das Bureau of Labor Statistics eine Zweijahresprognose der Entwicklung von Hunderten von Karrieren zwischen 2016 und 2026 veröffentlicht.


Auf der Grundlage der Prognosen und Schätzungen des durchschnittlichen Jahreseinkommens dieser Arbeitsplätze haben Forscher eine Rangliste der teuersten Berufe in der Zukunft veröffentlicht.
Hier werden 21 Stellen mit den höchsten Gehältern in den kommenden Jahrzehnten erwartet.
21. Landwirte, Viehzüchter, landwirtschaftliche Manager
20 Hauptaufgaben: Planen, verwalten, betreiben Sie den Betrieb, Gewächshaus, Aquakultur, Baumschule, Wald oder andere landwirtschaftliche Anlagen.
Anzahl der offenen Stellen im Jahr 2026: 68.700.
Durchschnittliches Einkommen im Jahr 2016: 66.360 USD.
Voraussetzungen: Abitur oder gleichwertig.
Computersystem-Analysator

Hauptaufgaben: Analyse von Datenverarbeitungsfrage…

The Flame Breathers

I write this narrative, not with the idea of contributing any additional scientific data to the discovery of Vulcan, but to put upon the record the real facts of our truly-amazing space voyage. The newscasters have hailed me as a modern Columbus. Surely I would not want to appear ungracious, unappreciative of all the applause that has been heaped upon me. But I do not deserve it. I did my job for my employers. The Society sent me to make a landing upon Vulcan—if the little planet existed. I found that it does exist; it was exactly where I was told it ought to be. I carried out my instructions, returned and made my report. There is no great heroism in that. So I am writing the facts of what happened. Just a bald, factual account, without the imaginative trimmings. The real hero of the discovery of Vulcan was young Jan Holden. He did his job—did it well—and he did something just a little extra. I'm Bob Grant, which of course you have guessed by now. Peter Torrence—the third member …

Der verschwindende Baum

Die Palme war einst am Stadtrand von Madurai reichlich vorhanden. Leider verschwindet der üppige Baum dank der raschen Verstädterung vom Horizont.
Raju legt seine Hände fest um den dunklen Kofferraum. Er befestigt den Knoten des Vadam um seine Beine und hüpft in weniger als fünf Minuten wie ein Frosch den 25 Fuß hohen Baum hinauf. Diese Bäume liegen mir sehr am Herzen. Ich umarme sie jeden Tag “, sagt Raju. Der Nungu-Verkäufer spricht von den Palmen, die für die Nungu-Verkäufer eine Einkommens- und Lebensgrundlage waren. Aber jetzt erscheint ihnen die Zukunft düster, da die Palmen rapide abnehmen. Die Stadt wächst und die Bäume werden gefällt, um Platz für Grundstücke zu machen, sagt Raju. „Früher war die Sivaganga Road von Palmen gesäumt, heute sind nur noch wenige übrig. Dies ist der Grund, warum der Preis für Palmen gestiegen ist. “

Umweltschützer sind auch besorgt über die sinkenden Zahlen. Die Panai Marams stammen aus dem südlichen Tamil Nadu und sind auch der Staatsbaum. "…