Solved

How to setup virtual subdomains with just Mod_rewrite and .Htaccess (+500 bonus)

Posted on 2006-11-13
6
754 Views
Last Modified: 2008-01-09
Hello and thank you for taking the time to read this question. Before I begin, as this question is  quite challenging i'm awarding an additional 500 bonus points to the person who can provide me a working .Htaccess file/code that achieves the desired result.

Let me begin.

Assume that website mydomain.com is hosted on a dedicated host with a static IP.
A wildcard domain entry has been added so that any subdomains are redirected to the domain name. For example: somesubdomain.mydomain.com/ --> redirects to mydomain.com/
Mydomain.com contains one index.html file in its root and is the default start page for the website.

What I want to achieve:

1. Any requests to subdomains must be redirected to my root domain path, however, the redirect process needs to be transparent to the users browser, thus the user must still see their original url they entered.

For example: user enters subx.mydomain.com, user is redirected transparently to mydomain.com/index.html, user is shown the same url as entered. subx.mydomain.com

Note that certain subdomains need to be excluded, for example www.

2. Should the user enter a non existent resource path, the user will be redirected to mydomain.com/index.html, again, this redirect needs to be transparent and the user shown the exact same url they entered.

For example: user enters www.mydomain.com/somedir1/somdir2/, user is transparently redirected to mydomain.com/index.html but still shown their original url www.mydomain.com/somedir1/somdir2/


Please feel free to ask questions should you require more information.

Best regards,

 Llewellyn
0
Comment
Question by:Loftbug
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 5

Expert Comment

by:wranlon
ID: 17931675
Assuming you're using Apache, Isn't this possible using a VirtualHost in the .conf file?
0
 
LVL 8

Expert Comment

by:netmunky
ID: 17932606
1) if you are using a wildcard, and have *.yourdomain.com in your ServerAlias in httpd.conf, then that will already work

2) you can add the directive:
ErrorDocument 404 /index.html
HOWEVER: the user will still get a 404 status code, along with your index.html content. depending on the browser, it may or may not display this html content (i believe IE only displays custom 404 html if the html content is over a specific size).
You can get around this by using index.php instead of index.html, and on the first line, add this:
<? header("HTTP/1.0 200 Ok") ?>
0
 
LVL 4

Author Comment

by:Loftbug
ID: 17936786
Hello Wranlon,

>> Assuming you're using Apache, Isn't this possible using a VirtualHost in the .conf file?
Yes, works well but not all hosts support making changes to the httpd.conf so we need to go with using .htaccess and Mod_rewrite.

Hello Netmunky,
>> ErrorDocument 404 /index.html HOWEVER: the user will still get a 404 status
Your idea makes sense but i'd rather not go this route. I've looked into what can be done with Mod_rewrite and it offers good extendability and portability.

Thanks.

0
 
LVL 4

Author Comment

by:Loftbug
ID: 17937540
I have found the solution by experimenting with mod_rewrite.

.Htaccess file placed in document root
 Options +Indexes
 Options +FollowSymlinks
 RewriteEngine On
 RewriteCond %{HTTP_HOST} ^(.+)\.domain\.tld$ [NC]
 RewriteRule ^$ /index.php?page=page2 [QSA,L]


Thank you for your comments. I'm going to request that this Question is closed.

 Llewellyn
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 18189410
PAQed with points refunded (500)

Computer101
EE Admin
0

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
replica website 2 70
Secure log in 'box' 4 42
wordpress theme 8 39
Selenium WebElement Login button findElement 9 12
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
This Micro Tutorial will demonstrate how to add subdomains to your content reports. This can be very importing in having a site with multiple subdomains.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question