Page 1 of 1

URL rewriting almost done

Posted: Thu Feb 10, 2011 5:12 am
by Sindarin
I am pretty new to url rewriting,

My url structure is like this http://www.site.com/index.php?section=news&id=30

and I need it to be http://www.site.com/news/30/

I have written down this .htaccess file,

Code: Select all

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)/(.*)/(.*) /index.php?section=$1&id=$2 [QSA]
RewriteRule ^(.*)/(.*) /index.php?section=$1&id=$2 [QSA]
RewriteRule ^(.*)/ /index.php?section=$1 [QSA]
It works, but the external files the page uses like .css/.js/images/favicon are not loaded!

Re: URL rewriting almost done

Posted: Thu Feb 10, 2011 8:52 am
by John Cartwright

Code: Select all

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)/(.*) /index.php?section=$1&id=$2 [QSA]
RewriteRule ^(.*)/(.*) /index.php?section=$1&id=$2 [QSA]
RewriteRule ^(.*)/ /index.php?section=$1 [QSA]
Which will not load files that are physically requested, and exist, on the filesystem. Although, I would not recommend using those rules, since it does not allow for much flexibility. Generally, you want to parse the url in PHP using a router.

Code: Select all

RewriteRule (.*) /index.php?url=$1 [QSA]
And your index.php will parse the url and dispatch accordingly.