?
Solved

mod rewrite subdomain path

Posted on 2008-10-21
11
Medium Priority
?
744 Views
Last Modified: 2012-05-05
hello,

my question about mod_rewrite condition. I'm trying to get if I write in adress bar "marlon.domain.com", it should show user's page in "www.domain.com/users/url=variable" with shown in adress bar as marlon.domain.com.  I set my server's wildcard and other settings. I'm using plesk 8.xx and Centos. First I configure write mode as :

RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com$ [NC]
RewriteCond %1 !^(www|ftp|mail|ns1|ns2|ns|en|tr)$ [NC]
RewriteRule ^.*$ http://www.domain.com/user/index.php?url=%1 [R,L]

as a result when I type in adress bar "marlon.domain.com" I get "www.domain.com/user/index.php?url=marlon". It is only redirection.

after that I found another rewrite rule :

RewriteCond %{HTTP_HOST} !^www.* [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com
RewriteCond /var/www/vhosts/domain.com/httpdocs/%1 -d
RewriteRule ^(.*) /%1/$1 [L]

It worked as marlon.domain.com as I wanted. but it's going just root of website. I coulnt figure it out how can I set the path "/users/url=marlon".

even I would figure it out,  I will have an another problem in this setup. my all links are like this :
<a href="/users/2">user</a>

so I just wanted to when anyone type in adress bar marlon.domain.com, he or she reach to the user's page. after that if they click another link in page they should go www.domain.com/anylink. but in this situation, when you click any link under marlon.domain.com, it's continue to show marlon.domain.com/anylink. But as you can guess I just want users to go www.domain.com/anylink after marlon.domain.com


It is difficult to tell actully but I hope I make myself clear.

Anyone have any idea on that?
0
Comment
Question by:middleofnowhere
[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
  • 5
  • 4
11 Comments
 
LVL 27

Accepted Solution

by:
caterham_www earned 2000 total points
ID: 22775245
Both domains are pointing to the same location/server, right? I.e. http://www.domain.com/user/ == http://sub.domain.com/user/
# userscript
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC]
RewriteCond %1 !=www
RewriteRule ^$ /user/index.php?url=%1 [L]
 
# redirect anything else but not the user script
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC]
RewriteCond %1 !=www
RewriteCond $1 !=user/index.php
RewriteRule ^(.+)$ http://www.domain.com/$1 [R,L]

Open in new window

0
 

Author Comment

by:middleofnowhere
ID: 22776188
yes both domain/subdomain on same server.

I tried this solution and it should be correct. But there is a problem on that. It's perfectly open the user's page as I wanted marlon.domain.com. but even though I logged in website as member, in users page it shows I haven't logged in. After that if I click any link www.domain.com/.. it looks that I'm logged in. also it doesnt load ajax content in users page. I wonder that because scripts try to use document root as subdomin's root? in other word do I have to change links in that page as http://www.domain.com/path?.

another problem is on my error pages. if I try to use wrong username, it goes error docs that I specified, but it looks like in adress bar : www.domain.com//notfound
there is double slash after domain name interestingly.  
0
 

Author Comment

by:middleofnowhere
ID: 22794802
do you have any additional idea on that? because I'm so close to that
0
Supports up to 4K resolution!

The VS192 2-Port 4K DisplayPort Splitter is perfect for anyone who needs to send one source of DisplayPort high definition video to two or four DisplayPort displays. The VS192 can split and also expand DisplayPort audio/video signal on two or four DisplayPort monitors.

 
LVL 27

Expert Comment

by:caterham_www
ID: 22800394
It looks like that this are problems which are not directly caused by mod_rewrite. Mod_rewrite does not handle the HTTP-response, so if you're logged in or not, seems to be a script problem (may be cookie/session and changing the domain causes problems with cookies).

http:/sub.domain.com/path is being redirected to http:/www.domain.com/path  by the 2nd rule. May be cross-domain ajax requests are a problem or XMLHttpRequest have problems, if the response is a redirect to http:/www.domain.com/path. You may also exclude that path from the 2nd rule by duplicating 'RewriteCond $1 !=user/index.php' with another path, of course.

> another problem is on my error pages.

And how do you issue that error? In your script via header("Location: http,......")?
0
 

Author Comment

by:middleofnowhere
ID: 22800665
I'm working with server side sessions. Actually I use on all javascript paths like /folder/xxx.php. For php sscripting I use generally require($_SERVER['document_root']."/folder/path"); line.

By the way my ht access file has many line about like :

RewriteRule event/ /events/event.php
RewriteRule event/([0-9]+) /events/event.php?id=$1




aslo error pages path
header("Location:/notfound")
on my scripts.
0
 

Author Comment

by:middleofnowhere
ID: 22800780
this is so intresting. I checked your htaccess rule out again and examined the situation with firebug on ajax requests. they are giving this result in firebug console :

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>302 Found</title>
</head><body>
<h1>Found</h1>
<p>The document has moved <a href="http://www.domain.com/user/ajax/portfolyo.php">here</a>.</p>
<hr>
<address>Apache/2.2.3 (CentOS) Server at marlon.domain.com Port 80</address>
</body></html>
0
 
LVL 27

Expert Comment

by:caterham_www
ID: 22800813
That is the HTTP-response body.  There should be a location header in the response headers, too, but the ajax engine seems not to follow the location header. So xclude the path /folder/ (I suppose /user/ajax/) from the rule, which redirects to www.domain.com.

$_SERVER['document_root'] should be the same unless you have different document_roots for your sub and non-subdomain.

> aslo error pages path
> header("Location:/notfound")

According to the specification, a location header must always look like "Location: http://example.com/foo" (fully qualified)
0
 

Author Comment

by:middleofnowhere
ID: 22800856
I dont have any real subdomains,  so document_root part is ok.
by the way I examine php script side actually. and all mysql queries are working.

$user = $_GET['urlusers'];
mysql_query("SELECT......WHERE user =$user ");

the lines above are working properly. but sessions have a problem :

if ($_SESSION['user']) {
$a =1
}

but it doesnt work. It seems registered $_SESSION variables are ignoring in sub.domain.com address.


by the way I'm sorry but I cannot fully understand that exclude path folder from the rule. when saying "rule" you are meaning htaccess, sub.domain.com goes directly root's index.php. so do I suppose to change ajax request paths?

like ajax/portfolyo.php instead of /user/ajax/portfolyo.php


0
 
LVL 27

Expert Comment

by:caterham_www
ID: 23213602
Why do you think that this question should be deleted? If the original question was answered "and it should be correct." and subsequent questions are asked which appeared to be script problems, the original questions should not be deleted.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

800 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