Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite directory and page url GET

         

thoford75

1:42 pm on May 10, 2012 (gmt 0)

10+ Year Member



I have edited my .htaccess file to divert:

something.domain.com => domain.com/hosted/something

with the exception of www.domain.com.

Before I had this in place, I had a rule which would rewrite (for example)

domain.com/contact-us => domain.com/page.php?url=contact-us and show domain.com/contact-us

Now what I need is when I'm on "something.domain.com/contact-us" (for example), it to go to "domain.com/hosted/something/page.php?url=contact-us" and to show "something.domain.com/contact-us"

My current htaccess file:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com
RewriteRule ^$ /hosted/%1 [L]

##### Do not process admin area #####
RewriteRule ^admin/?.*$ - [PT,L]

##### General redirect #####
RewriteRule ^([^/\.]+)/?$ page.php?url=$1 [QSA]

lucy24

8:24 pm on May 10, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ouch. When you start messing with (sub)domain names, you're venturing into proxy territory. Can you deal with that? (I'm guessing yes, since you've already got a rule with [PT] flag.)

I note by the way that your Conditions don't come with closing anchor, so they don't do anything about the (rare but attested) case where someone asks for "www.example.com:80" and the like.

Put the admin exception before the other rules. Then it's safely out of the way and you don't have to think about it again.

Now what I need is when I'm on "something.domain.com/contact-us" (for example), it to go to "domain.com/hosted/something/page.php?url=contact-us" and to show "something.domain.com/contact-us"

Is this the basic pattern you're looking at? (DO NOT cut & paste; this is preliminary.)

RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteCond %{HTTP_HOST} !^example\.com$
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule (.+) /hosted/%1/page.php?url=$1 [L]


Can the subdomain be anything at all, or is there a finite number of them? If there's only a small number, you can list them by name as (sub1|sub2|sub3)\.example\.com and skip the other Conditions. If they have a fixed length, or minimum or maximum length, include that: [^.]{2,} or [^.]{3,12} or similar. Is there anything preventing your subdomains from being named, say, wwwwww6.example.com? If not, there probably should be. You'll also want to block "example.example.com".

I put + rather than * in the Pattern assuming that you want a different rule when the request is simply for the (sub)domain front page. But if your php is set up to handle null requests ("url=") then it can all go in the same Rule.

thoford75

10:36 am on May 11, 2012 (gmt 0)

10+ Year Member



Managed to get it working with this:


RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule ^$ /hosted/%1 [L]

RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule ^([^/\.]+)/?$ /hosted/%1/page.php?url=$1 [QSA]

##### Do not process admin area #####
RewriteRule ^admin/?.*$ - [PT,L]



The only thing left to do is when sub.example.com is entered into the URL it is redirected to sub.example.com/hosted/sub (the correct directory!) I need the URL to just read sub.example.com/home


Thanks for your help so far!

lucy24

4:26 pm on May 11, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If it's getting redirected, then you have another rule somewhere else. A rewrite alone doesn't create a redirect. You may need to spend more time poring over your-- shudder-- proxy handling.

:: looking vaguely around for one of the three people who are brave enough to start flinging [P] flags around ::

What prevents the first two rules from processing requests for admin?