Need help for url rewriting

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
snowbydave1
Forum Newbie
Posts: 1
Joined: Sat Dec 05, 2009 5:02 am

Need help for url rewriting

Post 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
User avatar
ridgerunner
Forum Contributor
Posts: 214
Joined: Sun Jul 05, 2009 10:39 pm
Location: SLC, UT

Re: Need help for url rewriting

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