Forum Moderators: phranque

Message Too Old, No Replies

URL Re-writing

         

kishan

11:45 pm on Oct 28, 2010 (gmt 0)

10+ Year Member



Hi Guys,
I have been trying these piece of URL rewriting for my website i have done more trial and error stuff i need some help, all the direct re-writing is working fine
means when we type
www.abc.com/trolleys
it redirect to www.abc.com/product.php?idCategory=2

but if someone type
www.abc.com/product.php?idCategory=2 it doesn't redirect to /trolleys

If any one could help me with this it would be great and thanks in advance.

my .htaccess file is something like

RewriteEngine on

RewriteBase /

RewriteRule ^trolleys$ /trolleys/ [NC,R=301,L]
RewriteRule ^trolleys/$ /products.php?idCategory=2 [NC,L]

RewriteCond %{QUERY_STRING} ^idCategory=2$ [NC]
RewriteRule ^products\.php$ /trolleys/? [NC,R=301,L]

but its picking up query at the end for /trolleys?idCategory=2

jdMorgan

12:32 am on Oct 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




Wrong order, creates an 'infinite' loop, non-robust syntax....

RewriteEngine on
RewriteBase /
#
# Externally redirect direct client requests for script filepath+query to new SEF URL
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /products\.php\?idCategory=2\ HTTP/ [NC]
RewriteRule ^products\.php$ http://www.example.com/trolleys/? [NC,R=301,L]
#
# Externally redirect to add a trailing slash back of the trolleys
RewriteRule ^trolleys$ http://www.example.com/trolleys/ [NC,R=301,L]
#
# Internally rewrite SEF URL-path requests to script+query
RewriteRule ^trolleys/$ /products.php?idCategory=2 [NC,L]

The construct using THE_REQUEST in the first rule prevents the 'infinite' rewrite/redirect loop that occurs when you don't check to see if the script request is coming from the browser or coming from the last (internal script rewrite) rule.

Jim

kishan

12:46 am on Oct 29, 2010 (gmt 0)

10+ Year Member





You are just simply Gr8888..Thanks a lot

jdMorgan

12:58 am on Oct 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



See also Proper Order for htaccess [webmasterworld.com] for more info on rule-ordering.

Jim