Forum Moderators: phranque

Message Too Old, No Replies

Multiple Domains rewritten to folders that are rewritten.

         

Aharl

12:09 am on Sep 19, 2009 (gmt 0)

10+ Year Member



I've been working on this issue for a week or so and in searching google Ive read & stumbled upon lots of posts from here. Figured I would ask as my code may just need a minor tweak.

I think describing the issue in the terms of a social network site might help me explain it.

I have a website maindomain.com

Users of my site can create profiles that users can interact with. They get their own URL which is,
maindomain.com/alex
which is really
maindomain.com/index.php?user_slug=alex

When people interact with their profile they are seeing pages like,
maindomain.com/alex/view_images.php?view=all&sort=popular
which is really
maindomain.com/view_images.php?user_slug=alex&view=all&sort=popular

There is no folder named "alex", etc. on the server.

I have this working okay, but I wanted to enable people to have their own domain for their profile. So Alex would want AlexProfile.com to be used instead of maindomain.com/alex

I've parked AlexProfile.com at maindomain.com, and am currently using this code to handle it in my htaccess


RewriteCond %{HTTP_HOST} ^alexprofile.com
RewriteRule (.*) [alexprofile.com...] [R=301,L]

RewriteCond %{HTTP_HOST} ^www.alexprofile.com
RewriteCond %{REQUEST_URI} !^/alex
RewriteRule ^(.*)$ /alex/$1 [NC,L]

Then I have my htaccess code that handles turning the /alex/ into maindomain.com/view_images.php?user_slug=alex


RewriteRule ^([^./]+)$ /index.php?user_slug=$1 [NC,L,QSA]
RewriteRule ^([^./]+)/$ /index.php?user_slug=$1 [NC,L,QSA]

RewriteRule ^([^./]+)/([^./]+).php$ /$2.php?user_slug=$1 [NC,L,QSA]

So here is my whole htaccess,


RewriteEngine On
RewriteCond %{HTTP_HOST} ^maindomain.com
RewriteRule (.*) [maindomain.com...] [R=301,L]

RewriteCond %{HTTP_HOST} ^alexprofile.com
RewriteRule (.*) [alexprofile.com...] [R=301,L]

RewriteCond %{HTTP_HOST} ^www.alexprofile.com
RewriteCond %{REQUEST_URI} !^/alex
RewriteRule ^(.*)$ /alex/$1 [NC,L]

RewriteRule ^([^./]+)$ /index.php?user_slug=$1 [NC,L,QSA]
RewriteRule ^([^./]+)/$ /index.php?user_slug=$1 [NC,L,QSA]

RewriteRule ^([^./]+)/([^./]+).php(.*) /$2.php?user_slug=$1&$2 [NC,L,QSA]

With this htaccess maindomain.com functions fine but alexprofile.com yields an "Internal Server Error".

When I remove that last line of the htaccess and view alexprofile.com it gives me a "Not Found" error saying it cant find the folder /alex/index.php

I dont really understand why its trying to look in the physical folder. Shouldnt the htaccess rewrite it like it would if I was on maindomain.com/alex?

Thanks for taking the time to read all that.

jdMorgan

1:01 am on Sep 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If your intent is to capture a query string appended to x/y.php, the pattern in this rule won't work:

RewriteRule ^([^./]+)/([^./]+).ph[b]p(.*)[/b] /$2.php?user_slug=$1&$2 [NC,L,QSA]

The reason is that RewriteRule cannot 'see' querystrings, it only sees localized URL-paths. If you want to check the query string, then use a RewriteCond to examine %{QUERY_STRING}. I don't know why you'd bother though, since you're already using [QSA] (look it up if unclear).

---


RewriteRule ^([^./]+)$ /index.php?user_slug=$1 [NC,L,QSA]
RewriteRule ^([^./]+)/$ /index.php?user_slug=$1 [NC,L,QSA]

This is a mistake because it allows two URLs for the same 'page,' known as 'duplicate-content' and a popular issue for discussion in our search engine forums. Don't do this on purpose. Instead, pick either slashed or non-slashed URLs as your standard, and redirect the other type of URL to your standard-format URL. The [NC] flag on these rules is also completely unnecessary.

---

Unless you're selling domain names, I can't see this as a scalable approach -- You're going to have to add .htaccess code every time a new user signs up -- and that's going a lot of work if you are successful...

Have you considered simply using subdomains? With a cheap upgrade (~$12/year in the U.S.) to an IP-based shared server, you can have as many subdomains as you like, and the code to map them to filespace is not any more complicated than what you've already got.

---

Other than those mentioned, I can't see a problem with the code above, and I suspect you're hitting a well-known mod_rewrite bug, where URL-path-parts get duplicated improperly when two or more internal rewrites are applied to the same request. The solution is to rewrite from "alexprofile.com" straight to "/index.php?user_slug=alex" in a single rule, instead of doing it in a two-step process.

Jim

Aharl

2:43 am on Sep 19, 2009 (gmt 0)

10+ Year Member



Thanks Jim! This is working great for the alexprofile.com URL,

RewriteCond %{HTTP_HOST} ^www.alexprofile.com
RewriteCond %{REQUEST_URI} !^/alexprofile
RewriteRule ^([^./]+).php $1.php?user_slug=alexprofile [L,QSA]

I ran into an issue with $_Post variables not passing through until I realized they were passing through just getting removed by my redirect when htaccess adds "www." I was sending them through alexprofile.com instead of www.alexprofile.com

I removed that last line of my htaccess and cleaned up the rest so its unneeded.

Also you are right about selling the domains. Im going to resell/lease them to the users because it will enable their page/site to be standalone. And if their page is standalone others will take it more seriously and the more popular it gets the better for everyone. This project is still a good 6 months away from coming to fruition.

In the beginning I started creating the site so it was alex.maindomain.com but ran into lots of htaccess errors so I opted for the folder instead of subdomain. I think with what I've just learned I could get that working.

I haven't done it yet but I will go ahead and get rid of the duplicate content pages with a redirect for non slash/ to slash/.

But yeah I really appreciate the help, you saved me another night of banging my head against the wall so to speak.

jdMorgan

3:16 am on Sep 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, but be careful about selling this as a service. Using .htaccess to map domains to filepaths is not a secure way of doing things. At the very least look into using httpd.conf instead, so that you can lock things down a little more tightly. Best of all would be to use Apache as intended, and declare a <VirtualHost> for each user.

When picking slash versus non-slash, bear in mind Web standards... Things that end with slashes are "directories" and things that end without a slash are "pages" or "objects."

Jim