Forum Moderators: phranque
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.
RewriteRule ^([^./]+)/([^./]+).ph[b]p(.*)[/b] /$2.php?user_slug=$1&$2 [NC,L,QSA]
---
RewriteRule ^([^./]+)$ /index.php?user_slug=$1 [NC,L,QSA]
RewriteRule ^([^./]+)/$ /index.php?user_slug=$1 [NC,L,QSA]
---
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
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.
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