Rewrite conditions for URLs like this

How do I go about writing the conditions for mod_rewrite to make this example happen?

Have a URL this like...

http://domain.com/recent/5

Render this page...

http://domain.com/index.php?view=recent&page=5

And if there is no page number specified (note I want it to work with or without trailing-slash)...

http://domain.com/recent/

It will default to page 1...

http://domain.com/index.php?view=recent&page=1

But when someone just goes to the root domain...

http://domain.com

I want this page to render...

http://domain.com/index.php?view=popular&page=1

Thanks for any help.

Answers


Put this in the htaccess file in your document root:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([0-9]+) /index.php?view=$1&page=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /index.php?view=$1 [L,QSA]
RewriteRule ^$ /index.php?view=popular&page=1 [L,QSA]

Need Your Help

User Click Tracking Library for Desktop App

c# desktop-application user-input tracking user-tracking

We are looking into the possibility of allowing users to opt into a program where they report what button clicks etc. they do, and I was wondering if anyone can suggest a good library which already...

drawRect:/renderInContext: issues

ios uiview calayer exc-bad-access drawrect

Seem to be having a bit of an issue trying to draw my view into a context.