Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite or not?

         

SteveBarrese

6:52 pm on Feb 28, 2012 (gmt 0)

10+ Year Member



Hi there,

I have a site called www.test.com.

Users can register to site in order to have their private area:

User 1 -> www.test.com/user_1/home
User 2 -> www.test.com/user_2/home
User 3 -> www.test.com/user_3/home

A nav menu (Home, Products, Contacts) located in the private area homepage allows the visitor to reach the subdirectories of the private area:

www.test.com/user_1/home
www.test.com/user_1/products
www.test.com/user_1/contacts

Now I would like to give every user the chance to link a domain to their private area on www.test.com.
Example, let's suppose that User 1 holds the domain "user1.com", I would like to redirect the www.user1.com to www.test.com/user_1/home, and I would like that the URL in address bar remains www.one.com.

Also, once reached the homepage of the private area, the visitors can browse the site, so if they click on Products button, I would like that the URL in the address gets change into: www.user1.com/products

So basically www.user1.com/products corresponds to www.test.com/user_1/products

I tried mod_rewrite but I get always a kind of loop that leads to an Internal Server Error.

Anyone can help me on this?

Thanks a lot in advance.
L.

lucy24

10:43 pm on Feb 28, 2012 (gmt 0)

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



Where are all those domains coming from? You can do all kinds of stuff with subdomains-- even wildcard subdomains if your server is set up appropriately. But full-fledged domains are a whole nother issue.

If you could bring yourself to use the common pattern of

user1.example.com >> serves content from example.com/user1/home
user2.example.com >> serves content from example.com/user2/home

(details can change) there are several hundred earlier threads in this forum talking about how to do it.

But domains are serious business. You can do stuff with proxies and aliases-- but only if you're talking about a limited number of users, and domains that already exist.

SteveBarrese

12:11 am on Feb 29, 2012 (gmt 0)

10+ Year Member



Hi Lucy

Where are all those domains coming from?


Those are real domains, bought by users who register to the www.test.com site in order to link their private area.

The standard redirect service offered by hosting providers is not what I need since it takes advantage of an iframe which keeps the source URL always on top hiding the full path.

Now I opted out for a DNS service which allows to point the domain on a specific host by setting up the IP. Once set up the domain (user1.com) in order to point to the test.com host must be created a DNS zone on the "test.com" server relating to the user's domain, in our case "user1.com".

Now the tough job! I played with mod_rewrite in the .htaccess file in the root folder of the "user1.com" domain in order to reach the "test.com/user_1/home" every time a visitor typed "user1.com" in the address bar.

Redirect / [uesr1.com...]
RewriteRule (.*) [test.com...] [R=301,L]

RewriteRule ^user_([0-9]+)/$ privateareas/index.php?user_id=$1
RewriteRule ^user_([0-9]+)/home$ privateareas/index.php?user_id=$1
RewriteRule ^user_([0-9]+)/products$ privateareas/products.php?user_id=$1
RewriteRule ^user_([0-9]+)/contacts$ privateareas/contacts.php?user_id=$1

Redirect works correctly but I had no chance to keep the "user1.com" visible in the address bar. System always shows the destination URL in the address bar, test.com/user_1/home, and not the starting domain.

Hope all above makes sense.
L.

g1smd

1:03 am on Feb 29, 2012 (gmt 0)

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



Use RewriteRule for all of the rules otherwise you cannot guarantee processing order.

Make sure you have the [L] flag on every rule.

lucy24

1:28 am on Feb 29, 2012 (gmt 0)

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



Uh-oh, what's that "Redirect" doing there? That's a completely different directive from a completely different module. Get rid of it quick! (As usual I overlapped g1. His post says the same thing as this paragraph, but in different words.)

In any case, mod_rewrite can't "see" the domain name, so a RewriteRule by itself won't work. You have to do something with {HTTP_HOST}.

Is this your basic pattern?

Request for
www.user_name.com/blahblah

should serve content from
directory/otherdirectory/blahblah.php?user_id=name

Now, what about the other direction? If someone explicitly asks for
directory/otherdirectory/blahblah.php?user_id=name

do you shrug your shoulders and leave it that way, or do you want to redirect (address bar changes) to
www.user_name.com/blahblah

(Simplifying a little because the index.php and contact.php and so on are the smallest part of the problem.)

SteveBarrese

1:56 am on Feb 29, 2012 (gmt 0)

10+ Year Member



Ok Lucy, I got rid of it ;)

First of all thanks again (of course thanks to g1 for his/her help too).
About your pattern question, that should be:

Request for
www.user_name.com/blahblah

should serve content from
www.newsite.com/directory/otherdirectory/blahblah.php?user_id=name

About the other direction, if someone explicitly asks for
www.newsite.com/directory/otherdirectory/blahblah.php?user_id=name

Would be great to redirect (address bar changes) to www.user_name.com/blahblah, but I can shrug my shoulders and I leave it that way if it too hard to manage.

g1smd

7:54 am on Feb 29, 2012 (gmt 0)

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



No it's easy and it's one of the most popular questions in this forum, being asked at least once per week for the last ten years. Indeed someone asked the same question only yesterday.

There are very many prior example code snippets in this forum.

Basically the parameter redirect is done with a RewriteCond looking at THE_REQUEST in partnership with a RewriteRule and the internal rewrite is done with a standalone RewriteRule.

lucy24

10:07 am on Feb 29, 2012 (gmt 0)

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



The redirect is the easy part.

But doesn't the change in domain names cause complications on the rewrite end? You've got a request for domain A and you're serving content from domain B. Domain, not subdomain. Do we have to do something icky and messy involving [P] flags?

SteveBarrese

10:19 am on Feb 29, 2012 (gmt 0)

10+ Year Member



But doesn't the change in domain names cause complications on the rewrite end?


That is the main point on which I struggled with up to now...
Somebody also told me about the "path forwarding" but I'm not sure it could be the solution.

lucy24

9:52 pm on Feb 29, 2012 (gmt 0)

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



Yes, it's always a bad sign when Apache's instructions include something in a red box. In this case it's
Security Warning

Take care when constructing the target URL of the rule, considering the security impact from allowing the client influence over the set of URLs to which your server will act as a proxy. Ensure that the scheme and hostname part of the URL is either fixed, or does not allow the client undue influence.

Luckily I don't think this is a big danger point for you. It's the other half of the picture-- the redirecting-- that would potentially involve a nonexistent domain. No proxies involved. Your target URL on the rewrite side will always be the same domain-- and you know it exists.

Musing once again over Apache's example, which says-- verbatim, except that I changed "images" to "www"--

RewriteRule /(.*)\.(jpg|gif|png) http://www.example.com/$1.$2 [P]

:: cough, cough ::


Sit tight. We need someone who speaks Apache. (I don't. I only speak RegEx, which is 90% of most Rewrites. This is the other 10%.)

SteveBarrese

7:46 am on Mar 1, 2012 (gmt 0)

10+ Year Member



...that's what I suspected... a definitely tough job for me.

Lucy I would like to thank you for your help indeed.
Hope somebody could help me with the rest of the recipe.