URL rewriting almost done

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

URL rewriting almost done

Post 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!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: URL rewriting almost done

Post 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.
Post Reply