?
Solved

Apache mod rewrite for subdomains

Posted on 2010-09-16
20
Medium Priority
?
568 Views
Last Modified: 2013-11-19
I can I do mod rewriting to something like this,

http://user.mydomain.com, actual path is, mydomain.com/user.php?id=user

The rewriting is like a sub domain.

Is it possible ?
0
Comment
Question by:sahanz
[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
  • 7
  • 6
  • 2
  • +2
20 Comments
 
LVL 16

Expert Comment

by:HackneyCab
ID: 33693251
Well, I don't have much to do with subdomains, but you could try this:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([a-z]+)\.mydomain\.com$
RewriteRule ^$ http://mydomain.com/user.php?id=%1

This will capture any subdomain other than www and redirect requests to the root page to your user.php script.
0
 

Expert Comment

by:componentslab
ID: 33693328
Still the same :(

The subdomain I want to still use is HTTPS if it helps.

So all 3 goes to the new domain.

http://something.com
http://www.something.com
https://live.something.com
0
 
LVL 1

Author Comment

by:sahanz
ID: 33693361
not working :(
0
WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

 
LVL 16

Expert Comment

by:HackneyCab
ID: 33693363
componentslab, have you posted to the wrong thread? This question was opened by sahanz.
0
 
LVL 16

Expert Comment

by:HackneyCab
ID: 33693386
sahanz, what URLs are you trying which don't work? The directives I gave will only work for a request for the domain. If you request a file on the domain, then it will not redirect.
0
 
LVL 1

Author Comment

by:sahanz
ID: 33693407
ummm, I typed the direct URL. www.test.mydomain.com
0
 
LVL 16

Expert Comment

by:HackneyCab
ID: 33693442
That doesn't fit the pattern described in your question (that has three subdomains rather than two).

Can you describe a list of all possible types of pattern that need to be matched and redirected, and where they should be rewritten to (and any patterns which must not be redirected)?
0
 
LVL 1

Author Comment

by:sahanz
ID: 33693497
ok its like this, I have a php site and there is a members area. once user log in, site set up a cookie and then user can browse through the site. Lets say the main site domain is www.domain.com and we have 3 pages.

www.domain.com/view.php
www.domain.com/upload.php
www.domain.com/edit.php

So with rewriting what  I want is, to make it accessible to these 3 pages, using username as a subdomain.

www.user.domain.com/view.php
www.user.domain.com/upload.php
www.user.domain.com/edit.php
0
 
LVL 16

Expert Comment

by:HackneyCab
ID: 33693616
Right, but if someone requests www.user.domain.com/view.php in their browser, what is the path of the actual script you want called? Because you've now got two variables: username and filename. I'm guessing you need it to go to a file something like this:

mydomain.com/user.php?id=user&page=view.php

But you need to be explicit, because my mind reading ain't so hot.
0
 
LVL 1

Author Comment

by:sahanz
ID: 33700366
yes that correct,
mydomain.com/user.php?id=user&page=view.php

Sorry for beating around the bush :)
0
 
LVL 16

Expert Comment

by:HackneyCab
ID: 33701176
In that case, try:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([a-z]+)\.mydomain\.com$
RewriteRule ^([a-z]+\.php)$ http://mydomain.com/user.php?id=%1&page=$1

You may need to tweak the ([a-z]+\.php) pattern if your page names are more complicated than view.php (maybe including hyphens, underscores, etc).
0
 
LVL 1

Author Comment

by:sahanz
ID: 33701348
Hi, Seems its not working correctly.

my main domain, is http://tinyurl.com/39olmfw
I have this setupd under the domain, http://tinyurl.com/3y7naf3

so user.mydomain.com would redirect to, mydomain.com/test/user.php?id=user

0
 
LVL 1

Author Comment

by:sahanz
ID: 33846213
Do I need to enable something from apache? Wildcard subdomain or something?
0
 
LVL 17

Expert Comment

by:Thomas4019
ID: 34005263
it looks like it's possible to use rewriting, but the user will simply be "redirected" and not masked. So the URL will still be ugly. Why exactly do you want this?
0
 
LVL 17

Accepted Solution

by:
Thomas4019 earned 1000 total points
ID: 34005268
0
 
LVL 18

Assisted Solution

by:Mark Gilbert
Mark Gilbert earned 1000 total points
ID: 34005280
Make sure that AllowOverride All is in the virtual host config file. Then in your site make sure you have an htaccess looking for the username in the sub domain string to then convert it into your php get call.

0
 
LVL 1

Author Closing Comment

by:sahanz
ID: 34126830
Thanks, seems I need root access.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

777 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