• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 745
  • Last Modified:

mod rewrite subdomain path

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
middleofnowhere
Asked:
middleofnowhere
  • 5
  • 4
1 Solution
 
caterham_wwwCommented:
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
 
middleofnowhereAuthor Commented:
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
 
middleofnowhereAuthor Commented:
do you have any additional idea on that? because I'm so close to that
0
Industry Leaders: 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!

 
caterham_wwwCommented:
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
 
middleofnowhereAuthor Commented:
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
 
middleofnowhereAuthor Commented:
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
 
caterham_wwwCommented:
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
 
middleofnowhereAuthor Commented:
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
 
caterham_wwwCommented:
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now