• Home
  • Archive
  • Tools
  • Contact Us

The Customize Windows

Technology Journal

  • Cloud Computing
  • Computer
  • Digital Photography
  • Windows 7
  • Archive
  • Cloud Computing
  • Virtualization
  • Computer and Internet
  • Digital Photography
  • Android
  • Sysadmin
  • Electronics
  • Big Data
  • Virtualization
  • Downloads
  • Web Development
  • Apple
  • Android
Advertisement
You are here:Home » How to Automatically Redirect Subdirectory to Domain (Apache 2.4)

By Abhishek Ghosh November 23, 2018 8:52 pm Updated on November 23, 2018

How to Automatically Redirect Subdirectory to Domain (Apache 2.4)

Advertisement

In professional shared hosting, many a times user directory is redirected towards a subdomain. They usually acheive it via some scripting langugage like Perl, PHP with or without MySQL like database. However, we can use just Apache module mod_rewrite‘s dynamic mass virtual hosts feature to acheive near same result. Here is how to automatically redirect subdirectory to domain with Apache 2.4.

 

Automatically Redirect Subdirectory to Domain

 

First, it is important to note that we are using non-HTTPS sites for this guide. Second, we need a cloud based DNS service who supports wildcard entry. If we have content of www.abhishek.thecustomizewindows.com at /var/abhishek/www sub-directory path, then the system will redirect to www.abhishek.thecustomizewindows.com. If second entry is www.ghosh.thecustomizewindows.com at /var/ghosh/www sub-directory path, then the system will redirect to www.ghosh.thecustomizewindows.com. It is like permalink rewriting but with subdomain. As it is with subdomain, we need the reliability of DNS for faster resolution. In our example, *.thecustomizewindows.com will point to either main server IP or alternate IP. Easiest example of that configuration is :

Vim
1
2
3
4
RewriteEngine on
RewriteMap lowercase int:tolower
RewriteCond %{lowercase:%{HTTP_HOST}} ^www\.([^.]+)\.thecustomizewindows\.com$
RewriteRule ^(.*) /var/%1/www$1

tolower directive is used to make sure that the hostnames used will be all lowercase to avoid ambiguity in the directory structure. We are adding the stanza on virtual host file. Apache official site has good documentation on dynamic mass virtual hosts :

Advertisement

---

Vim
1
https://httpd.apache.org/docs/2.4/vhosts/mass.html

How to Automatically Redirect Subdirectory to Domain (Apache 2-4)

We can use the directives of Apache Module mod_vhost_alias :

Vim
1
https://httpd.apache.org/docs/2.4/mod/mod_vhost_alias.html

We can use a separate configuration file named vhost.map with content like this to point which subdomain will point where:

Vim
1
2
3
4
customer-1.thecustomizewindows.com /www/customers/1
customer-2.thecustomizewindows.com /www/customers/2
# ...
customer-N.thecustomizewindows.com /www/customers/N

In that case, our main configuration will be like following:

Vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
RewriteEngine on
 
RewriteMap lowercase int:tolower
 
# define the map file
RewriteMap vhost txt:/www/conf/vhost.map
 
# deal with aliases as above
RewriteCond %{REQUEST_URI} !^/icons/
RewriteCond %{REQUEST_URI} !^/cgi-bin/
RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$
# this does the file-based remap
RewriteCond ${vhost:%1} ^(/.*)$
RewriteRule ^/(.*)$ %1/docs/$1
 
RewriteCond %{REQUEST_URI} ^/cgi-bin/
RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$
RewriteCond ${vhost:%1} ^(/.*)$
RewriteRule ^/(.*)$ %1/cgi-bin/$1 [H=cgi-script]

There are complex ways of achieving the result, such as using RewriteMap Directive with some scripting :

Vim
1
https://httpd.apache.org/docs/2.4/vhosts/mass.html

Hope it will help.

Tagged With apache2 domain with subdirectory , redirect domain sub directory to another domain subdirectory
Facebook Twitter Pinterest

Abhishek Ghosh

About Abhishek Ghosh

Abhishek Ghosh is a Businessman, Surgeon, Author and Blogger. You can keep touch with him on Twitter - @AbhishekCTRL.

Here’s what we’ve got for you which might like :

Articles Related to How to Automatically Redirect Subdirectory to Domain (Apache 2.4)

  • Nginx WordPress Installation Guide (All Steps)

    This is a Full Nginx WordPress Installation Guide With All the Steps, Including Some Optimization and Setup Which is Compatible With WordPress DOT ORG Example Settings For Nginx.

  • WordPress & PHP : Different AdSense Units on Mobile Devices

    Here is How To Serve Different AdSense Units on Mobile Devices on WordPress With PHP. WordPress Has Function Which Can Be Used In Free Way.

  • Changing Data With cURL for OpenStack Swift (HP Cloud CDN)

    Changing Data With cURL For Object is Quite Easy in OpenStack Swift. Here Are Examples With HP Cloud CDN To Make it Clear. Official Examples Are Bad.

  • 301 Redirect by .htaccess : 301 Redirect Codes for Various Situations

    301 Redirect by .htaccess is most commonly used method for LAMP setup. 301 Redirect can be done by other methods, but .htaccess gives the best header response.

performing a search on this website can help you. Also, we have YouTube Videos.

Take The Conversation Further ...

We'd love to know your thoughts on this article.
Meet the Author over on Twitter to join the conversation right now!

If you want to Advertise on our Article or want a Sponsored Article, you are invited to Contact us.

Contact Us

Subscribe To Our Free Newsletter

Get new posts by email:

Please Confirm the Subscription When Approval Email Will Arrive in Your Email Inbox as Second Step.

Search this website…

 

Popular Articles

Our Homepage is best place to find popular articles!

Here Are Some Good to Read Articles :

  • Cloud Computing Service Models
  • What is Cloud Computing?
  • Cloud Computing and Social Networks in Mobile Space
  • ARM Processor Architecture
  • What Camera Mode to Choose
  • Indispensable MySQL queries for custom fields in WordPress
  • Windows 7 Speech Recognition Scripting Related Tutorials

Social Networks

  • Pinterest (24.3K Followers)
  • Twitter (5.8k Followers)
  • Facebook (5.7k Followers)
  • LinkedIn (3.7k Followers)
  • YouTube (1.3k Followers)
  • GitHub (Repository)
  • GitHub (Gists)
Looking to publish sponsored article on our website?

Contact us

Recent Posts

  • Hybrid Multi-Cloud Environments Are Becoming UbiquitousJuly 12, 2023
  • Data Protection on the InternetJuly 12, 2023
  • Basics of BJT TransistorJuly 11, 2023
  • What is Confidential Computing?July 11, 2023
  • How a MOSFET WorksJuly 10, 2023
PC users can consult Corrine Chorney for Security.

Want to know more about us?

Read Notability and Mentions & Our Setup.

Copyright © 2023 - The Customize Windows | dESIGNed by The Customize Windows

Copyright  · Privacy Policy  · Advertising Policy  · Terms of Service  · Refund Policy