Page 1 of 1

Rewriting URLs for Multiple Levels URL

Posted: Mon Jan 02, 2006 11:24 pm
by cent
Hi,

What I am trying to do is based off of code I've seen on here but I need to take it a step further. The 1st one allows people to put in http://www.thesite.com/username and it will load their profile without changing the URL.

Now the 2nd one is a little more trickier. What I want to have is:
http://www.thesite.com/username/blog/
which points to:
http://www.thesite.com/users/blog.php?name=$1

Where $1 would be the username to look for.

The 3rd one would be a lot more trickier with:
http://www.thesite.com/username/blog/th ... ntry_title
which points to:
http://www.thesite.com/users/blog.php?name=$1&blog=$2

Where $1 is the username and $2 would be the blog entry title that I can search for.


I have the following code in my htaccess file:

Code: Select all

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ /users/profile.php?name=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/$ /users/blog.php?name=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ /users/blog.php?name=$1&blog=$2 [L]

I'm a little at a lost as everything I've learned from doing these is based on what other's have done. And I have yet to find a multi-level rewrite rule nor do I fully understand them. So both of those combined makes for a very confused person.

Any help is absolutely, greatly appreciated.

Thanks. Cent.

Posted: Tue Jan 03, 2006 1:10 am
by josh
I always found reversing them so the longest rule is first helped

Code: Select all

# the rewrite rule for the category named "something"
RewriteRule	^category$			category/		[R]
RewriteRule	^category/(.*)/(.*)/$		browse.php?category=something&country=$1&prov=$2
RewriteRule	^category/(.*)/$			browse.php?category=something&country=$1
RewriteRule	^category/$			browse.php?category=something

Posted: Thu Jan 19, 2006 3:51 pm
by cent
hi jshpro2,

between your suggestion and fixing my logic I got it to work....

Thanks!
Cent

Posted: Thu Jan 19, 2006 10:56 pm
by josh
glad to help, there's also rewrite mappings for letting a perl / php script do the rewriting for you (the rewrite syntax can get complex when you need to nest things, or conditionally rewrite stuff depending on contents of a database, etc.)