Solved

mod rewrite subdomain path

Posted on 2008-10-21
11
737 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
  • 5
  • 4
11 Comments
 
LVL 27

Accepted Solution

by:
caterham_www earned 500 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
 
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

If you are running a LAMP infrastructure, this little code snippet is very helpful if you are serving lots of HTML, JavaScript and CSS-related information. The mod_deflate module, which is part of the Apache 2.2 application, provides the DEFLATE…
Hi, in this article I'm going to teach you how to run your own site, and how to let people in (without IP). I'll talk about and explain each step... :) By the way, everything in this Tutorial is completely free and legal. This article is for …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now