Welcome to WebmasterWorld Guest from 54.162.157.249

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

htaccess to write 2 rules with virtual subdomains

     
6:06 pm on Nov 4, 2012 (gmt 0)

5+ Year Member



I have virtual sub domains active and first part works fine.

This is my code
# Extract the subdomain part of domain.com
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com [NC]

# Check that the subdomain part is not www and ftp and mail
RewriteCond %1 !^(www|ftp|mail)$ [NC]

# Redirect all requests to a php script passing as argument the subdomain
RewriteRule ^.*$ http://www.domain.com/channel/?uid=%1 [R,L]


The above code works fine by redirecting the user with /channel page having the uid variable on it.

I need to do one more thing here.
I have a record folder on root of my domain. And that record folder also being accessed by supplying a uid parameter.

What I want to do is if a user types a subdomain with record folder, it should redirect too.

Example [vsubdomain.mydomain.com...] it should redirect to [mydomain.com...]

right now, it only works for the first part which example code I pasted above.

Please help.

thanks
8:50 pm on Nov 4, 2012 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Please use example.com in this forum so your example URL can be read.

Despite this instruction being in the sticky thread at the top of this forum, it is frustrating that only five (5) people have managed to comply with it so far this year.
12:38 am on Nov 5, 2012 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com [NC]
RewriteCond %1 !^(www|ftp|mail)$ [NC]

Deep philosophical question here. Or, at least, question about mod_rewrite nuances. Same difference.

Apache says:
RewriteCond backreferences: These are backreferences ... which provide access to the grouped parts (again, in parentheses) of the pattern, from the last matched RewriteCond in the current set of conditions

Now, technically the last matched Condition is the one that says "the previously captured bit is not (stuff in parentheses)".*

So when they say "last matched Condition" do they really mean "last matched positive Condition"? What if the last matched Condition has no parentheses, or only non-capturing parentheses? mod_rewrite is not the place for fuzziness.


* You and I know that the parentheses are just there to support the pipes. But mod_rewrite doesn't know that.
12:49 am on Nov 5, 2012 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



If the first condition doesn't match, e.g. for all non-www requests, the ruleset doesn't run at all. But that's probably exactly what's wanted anyway.
6:05 am on Nov 5, 2012 (gmt 0)

10+ Year Member



Why don't you put a separate .htaccess file in your http://www.example.com/vsubdomain folder?
7:32 am on Nov 5, 2012 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



If the first condition doesn't match, e.g. for all non-www requests, the ruleset doesn't run at all. But that's probably exactly what's wanted anyway.

I meant the %1 in the Rule. The one in Condition #2 makes perfect sense and doesn't contradict what Apache says. It's the one in the Rule that doesn't seem to fit.

Why don't you put a separate .htaccess file in your http://www.example.com/vsubdomain folder?

My impression was that the subdomains don't physically exist, so there's no folder to put an htaccess in.
10:18 am on Nov 5, 2012 (gmt 0)

10+ Year Member



May be.. but he can create it.

I think server will look for .htaccess file in the related folder first and if there is no such file then it will look for same in web root (Please correct me if I am wrong.) So if he create a folder and put a .htaccess file there with simple redirect code then it will be simple solution. Plus less work for the server, as it will find the file in that folder. And that .htaccess file will have just one simple redirect rule so again it saves server from dealing with other rules.
10:32 am on Nov 5, 2012 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



You are wrong. Sorry. Once the server learns-- through seeing an Overrides directive in the config file-- that htaccess is a possibility, it will look for an htaccess file in every directory it passes through, from top to bottom. In shared hosting that means:

userspace
domain root directory
physical directories within the root
next layer of physical directories

and so on until it arrives at the file. Every single one of those might contain an htaccess.

In the case of subdomains, the DNS in combination with the server administrator (your host, usually) will point the request toward a particular physical location. It might be the domain root directory, or it might be a subdirectory. But the request doesn't teleport straight to its destination. It has to pass through all the surrounding directories, starting with the config file.

It's like a human going to an office on the 2nd floor of a building. You can't just zip up the fire escape and climb in the window even if you know exactly where it is. You have to get past the security guard in the lobby, and the one outside the elevators, and the receptionist outside your target office. And it's no use telling the downstairs guard that the upstairs receptionist knows you. If you don't have the right credentials, you'll never get that far.
3:24 pm on Nov 5, 2012 (gmt 0)

10+ Year Member



Thank you lucy for correcting me. Now I know that it inherit the parent file.

Isn't there any way to tell the server that it shouldn't inherit the parent file?
5:18 pm on Nov 5, 2012 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



No. Inheritance is an Apache absolute. It always works from top to bottom. One of the few things that isn't inherited is the RewriteEngine; that has to be turned on separately in each htaccess that uses it.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month