homepage Welcome to WebmasterWorld Guest from 54.197.130.16
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
htaccess to write 2 rules with virtual subdomains
hozyali




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

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

 

g1smd




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

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.

lucy24




msg:4515690
 12:38 am on Nov 5, 2012 (gmt 0)

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.

g1smd




msg:4515692
 12:49 am on Nov 5, 2012 (gmt 0)

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.

Dinkar




msg:4515762
 6:05 am on Nov 5, 2012 (gmt 0)

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

lucy24




msg:4515787
 7:32 am on Nov 5, 2012 (gmt 0)

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.

Dinkar




msg:4515809
 10:18 am on Nov 5, 2012 (gmt 0)

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.

lucy24




msg:4515815
 10:32 am on Nov 5, 2012 (gmt 0)

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.

Dinkar




msg:4515893
 3:24 pm on Nov 5, 2012 (gmt 0)

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?

lucy24




msg:4515929
 5:18 pm on Nov 5, 2012 (gmt 0)

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved