.htaccess mod_rewrite mindblock

Tag: .htaccess , mod-rewrite , apache2 , query-string Author: chutian77585210 Date: 2011-08-30

I've been having trouble with the ModRewrite on how to appropriately hide query string parameters. Basically something like this..

from:

http://localhost/index.php?page=news&=request=somepage&id=100&sort=asc

to:

http://localhost/news/somepage/id/asc

I've attempted to do different variations with it but to no success. For now I make do with revealing query paramaters from the url which is what I'm trying to avoid.

Best Answer

Something like this

in .htaccess file write:

RewriteEngine on
RewriteBase /
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?page=$1&=request=$2&id=$3&sort=$4 [L]

Also for better using with images, stylesheets, scripts etc. I recommend to add <base href="index.php"> in <head> tag at all pages in your site.

comments:

Forgot to mention that some assets wont load like images and stylesheets because of the directories (I've done the same but with a different RewriteRule)..
I've edited my answer, try to add RewriteBase in your .htaccess file
<base href="index.php"> actually worked for me without declaring RewriteBase, although for it to work relatively I will have to work on adopting <base/> for deep addresses. Thanks :)