Chris
Chris

Reputation: 889

URL Rewrite best practice & how to set it up with htaccess

I have a small personal project I'm working on, and before I get too deep into it, I'd like to get some opinions here on the best practices regarding the set up and URL scheme... and how I might set up the .htaccess rewrites for the following. My questions are:

  1. Is the below set up the best way to manage user information in a small user profile setup?

  2. I have worked out how to get the subdomain working for the rewrite username.domain.com with wildcard subdomains and httpd.conf file set up... but I am stuck on the rest of the scheme seen below. Basically, a user profile will always result in username.domain.com and then appended with the various pages within their account (photos, videos, notes etc). How would I set up the .htaccess rewrites to accommodate this? I really appreciate any advice here. I have done a ton of research here on stackoverflow, and on other sites, but I can't find a decent explanation to achieve this.

Thanks for any help.

www.domain.com/profile.php?u=username --> username.domain.com

www.domain.com/photos.php?u=username --> username.domain.com/photos

www.domain.com/photos.php?u=username&a=album --> username.domain.com/photos/album

www.domain.com/photos.php?u=username&a=album1&p=photoid --> username.domain.com/photos/album1/photoid

www.domain.com/settings.php?u=username --> username.domain.com/settings

etc

Upvotes: 1

Views: 1581

Answers (2)

Cango
Cango

Reputation: 100

Wich server do you use? And first of all use rewrites in the config file of your server, not htaccess, htaccess slows the server down.

Edit: Iam not shure about the speed, but as far as i remember htaccess slows down apache, i dont know about how much. Iam sry you have to google that :)

Upvotes: 0

Ansari
Ansari

Reputation: 8218

Your proposed setup looks fine to me. Here are some rules for .htaccess (make sure you have mod_rewrite enabled and AllowOverride All set in your httpd.conf):

RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} (.*)\.domain\.com
RewriteRule ^$ http://www.domain.com/profile.php?u=%1 [L,QSA]

RewriteRule ^photos/?$ http://www.domain.com/photos.php?u=%1

RewriteRule ^photos/([^/]+)/?$ http://www.domain.com/photos.php?u=%1&a=$1

RewriteRule ^photos/([^/]+)/([^/]+)/?$ http://www.domain.com/photos.php?u=%1&a=$1&p=$2

RewriteRule ^settings/?$ http://www.domain.com/settings.php?u=%1

Upvotes: 2

Related Questions