Page 1 of 1

Need help for url rewriting

Posted: Sat Dec 05, 2009 5:04 am
by snowbydave1
Dear friends,


I want to do rewriting of url. In general we type url in following way.
http://www.domain.com/pagename.php?id=1 ... &kw=search

This url is shown as a
http://www.domain.com/pagename/id/123/p ... /kw/search

Is anybody give me Rewrite Rule for this ?


Thanking you,

Snowby Dave

Re: Need help for url rewriting

Posted: Sat Dec 12, 2009 12:24 pm
by ridgerunner
This should do the trick:

Code: Select all

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ $1.php?$2=$3&$4=$5&$6=$7
</IfModule>
Although this works for the example you provided (exactly one .php filename, three query parameter names and three corresponding values), this seems to be a strange request. You have made the name of the .PHP file (the first path) a variable (not smart). You have also made the names of the following three parameters variable. This is not smart because this rewrite rule will result in a boatload of "file not found" errors for those URL addresses that do not have corresponding .PHP files and the PHP code will need to handle $_GET variables having any names. Strange!

If on the other hand, you actually intended for the .PHP filename and query variable names to be fixed, then the following rewrite rule may be more in line with what you are after:

Code: Select all

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^pagename/id/([^/]+)/param2/([^/]+)/kw/([^/]+)$ pagename.php?id=$1&param2=$2&kw=$3
</IfModule>
Good luck!