Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

Need Help on htaccess code to redirect dynamic URL into static

Looking to convert a dynamic URL into search engine friendly URL

6:21 am on May 2, 2015 (gmt 0)

New User

joined:May 2, 2015
votes: 0

Dear All,

I am new member at this forum but using this site since long back. This time I am looking for custom help on .htaccess code to redirect my website URLs into static one. I will appreciate if someone can help on this

Below is current live link -


Same is going in all categories

I want to convert it into


Currently I am using

RewriteRule ^products/([a-zA-Z0-9-_%/,]+)/([a-zA-Z0-9-_%/,]+)/?$ categories.php?category=$1&parent_id=$2 [L,QSA]
RewriteCond %{THE_REQUEST} /categories\.php\?category=([^\s&]+)&parent_id=([^\s&]+) [NC]
RewriteRule ^ products/%1/%2? [R=301,L]

But its not helping me

Please help me guys

[edited by: Ocean10000 at 2:23 pm (utc) on May 2, 2015]
[edit reason] Examplified [/edit]

4:52 pm on May 2, 2015 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
votes: 890

But its not helping me

Somewhere there's a list of Phrases We Never Want To See in the apache subforums ;) Others include "it doesn't work" and "no joy". What, exactly, happens? ("Nothing at all happens" is a valid answer.)

Put a blank line after each RewriteRule so people coming along later-- including readers of this forum, and also yourself next year when you need to tweak the rule-- can see where one ruleset ends and another begins.

Constrain the body of the rule to the actual path that originally held the query string, like /products/ or /categories.php. (It's your site, so you know what the possibilities are.) Since you are rewriting to a different format than the old one, you don't need %{THE_REQUEST}, just %{QUERY_STRING}. This, in turn, makes the Condition easier to set up.

Put all external redirects before all internal rewrites unless there is a specific reason to put a rule in the "wrong" place. There shouldn't be any need for QSA, since the whole point was to remove the old query string from all requests, so there shouldn't be one at all. (And if for some reason there is one, it's reappended by default if you don't say anything to the contrary.) There is also no need for NC-- and it makes more work for the server, so omit it.


You can eliminate the \s since that was only needed when looking at the whole request. Once you're looking at the query string alone, there will be no spaces and it's a simple [^&]+

Are "category" and "parent_id" always the first two parameters in your old-style URLs with query? Or, at least, are they always adjacent? Once you're looking at %{QUERY_STRING} alone, you can omit the closing anchor, so it won't matter if some URLs have other stuff like "sort order" tacked on to the end. The rule will still be more efficient if you can use a ^ opening anchor.

RewriteCond %{QUERY_STRING} ^category=([^&]+)&parent_id=([^&]+)

Is there any possibility of empty parameters, such as "category=&parentid=" ? If yes, you'll need to figure out how to redirect them. If it doesn't occur, you may not need to worry about it.

Now, about the rewrite-- the rule you currently have first, but it needs to go second:

RewriteRule ^products/([a-zA-Z0-9-_%/,]+)/([a-zA-Z0-9-_%/,]+)/?$ categories.php?category=$1&parent_id=$2 [L,QSA]

What the heck are those % percent signs doing in the URL? They can only be an artifact of percent-encoding stuff in the old query string. If your old queries did contain non-ASCII characters that needed to be encoded, and for some reason they didn't get disencoded, you'll need some supplementary business to process them before they go into the URL. If the object was to make shorter prettier URLs, you won't achieve that by littering the path with percent signs. Also commas? Really?

In any case the whole package [[a-zA-Z0-9_] (alphanumerics plus lowline but not hyphen) can be expressed as \w which also includes any non-ASCII letters such as or if you've got them. Or you could just say [^/]+ the same way you do in capturing from a query.

Don't have an optional / at the end. Otherwise you've created Duplicate Content all over the site, because search engines will ask for URLs with trailing slash any time they see one without. (And vice versa.) If you've chosen to go extensionless, redirect any requests that do have a final / slash.