Changing url's

Using mod_rewrite to handle various content issues

Changing url's

Postby Jonna » Wed Apr 25, 2007 2:19 am

I'm new to mod_rewrite, and hoped someone could help me with an issue.

I'm setting up a site where different users/companies can have their own account. To see the different users on the site you have to type something like: index.php?company=xxx&page=xxx.....and so on. My problem is that I want the users to be able to type just /+ their username or company.

Example: www.mysite.com/mycompany instead of www.mysite.com/index.php?company=mycompany.

One solution would be to create a folder for every user with an index file that redirects the user , but I don't think it's a good one. If anyone have a good solution to this problem I would be thankful.
Jonna
 
Posts: 5
Joined: Wed Apr 25, 2007 1:32 am

Postby richardk » Wed Apr 25, 2007 5:09 am

In a .htaccess file in your document root
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /index.php?company=$1 [QSA,L]

Then you should be able to go to /company.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Jonna » Wed Apr 25, 2007 6:22 am

Thank you for your reply :D

I just have to other question:

The parent url string (without mod_rewrite) will probably have many parameters. How does this affect the url? Do I have to write a new condition when there's a new parameter in the url string, or is there a more dynamic way to solve this?

Secondly. If I have folders under the root that I don't want to be affected by the condition, what do I do? For example, I don't want an img catalog to be affacted (www.mysite.com/img)
Jonna
 
Posts: 5
Joined: Wed Apr 25, 2007 1:32 am

Postby richardk » Wed Apr 25, 2007 6:38 am

The parent url string (without mod_rewrite) will probably have many parameters. How does this affect the url? Do I have to write a new condition when there's a new parameter in the url string, or is there a more dynamic way to solve this?

/mycompany/?def=ghi would be rewritten to /index.php?company=mycompany&def=ghi. Or you could get rid of the query string completly, if you want to do that provide some examples.

Secondly. If I have folders under the root that I don't want to be affected by the condition, what do I do? For example, I don't want an img catalog to be affacted (www.mysite.com/img)

It shouldn't match any real files or directories (that's what the RewriteConds do).
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Jonna » Wed Apr 25, 2007 6:53 am

Thanks alot. You saved me some time there :D
Jonna
 
Posts: 5
Joined: Wed Apr 25, 2007 1:32 am


Return to Content

Who is online

Users browsing this forum: No registered users and 1 guest

cron