views:

74

answers:

2

Hey all, how can I reduce this:

www.example.com/index.php?page=viewblog&category=awesome

down to this:

www.example.com/blog/awesome

The above lists all of the blog posts in that category, but I also want scope for adding the title of the post on the end of it as well, like this:

www.example.com/index.php?page=viewblog&category=awesome&post=why-trees-are-green

And this needs to shorten down to:

www.example.com/blog/awesome/why-trees-are-green

Any ideas, anyone? Thanks in advance :)

+2  A: 
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f    
RewriteRule blog/([^/]+)/?$ index.php?page=viewblog&category=$1 [L]
RewriteRule blog/([^/]+)/([^/]+)/?$ index.php?page=viewblog&category=$1&post=$2 [L]

The condition makes sure the requested URL is not an actual file, which you want to serve (css, images, etc.)

Then, one rule per level, the first one will rewrite http://yourserver.com/blog/whatever to http://yourserver.com/index.php?page=viewblog&category=whatever

The second one will rewrite http://yourserver.com/blog/whatever/whenever to http://yourserver.com/index.php?page=viewblog&category=whatever&post=whenever

If you need more levels, add more rules accordingly.

Vinko Vrsalovic
Awesome. Thanks for the comprehensive and quick answer :)
Tim
@Vinko Vrsalovic it seems to work without RewriteCond. i didn't use it, and works fine.what RewriteCond do?
Syom
Vinko Vrsalovic
understeded. Thanks ;)
Syom
A: 

Something like this?

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^./]+)/([^./]+)$ index.php?page=view$1&category=$2 [L]
RewriteRule ^([^./]+)/([^./]+)/([^./]+)$ index.php?page=view$1&category=$2&post=$3 [L]
cam8001