Welcome to WebmasterWorld Guest from 18.212.222.217
Forum Moderators: Ocean10000 & phranque
We would like
www.domain.com/widgets/page.htm
to be served by
www.domain.com/script.php?pg=page&cat=widgets
We put this in our htaccess file:
RewriteEngine on
RewriteRule ^/widgets/(.*)\.htm$ /solar.php?pg=$1&page=widgets
We get a 404
What am I doing wrong?
Also, when I get this working, I want to do the same thing for bluewidgets, redwidgets etc.
Do I need separate RewriteRules for each or is there a way to make it a variable (instead of just "widgets"), but only with certain values (bluewidgets, etc)
Thanks!
RewriteRule ^widgets/(.*)\.htm$ /solar.php?pg=$1&page=widgets
without the leading slash which is stripped off when the RewriteRule is used in directory context.
RewriteRule ^(red¦blue¦green)widgets/(.*)\.htm$ /solar.php?pg=$2&page=$1widgets
should work for all kinds of widgets.
Andreas
It worked!
Can you please help with with another problem:
I got a little braver & tried to rewrite
www.domain.com/widgets/page.htm?id=xyz
to be served by
www.domain.com/script.php?pg=page&cat=widgets&id=xyz
I used:
RewriteRule ^widgets/(.*)\.htm?id=(.*)$ /solar.php?pg=$1&cat=widgets&id=$2
It didn't work. id=xyz was not in the final query string.
Can this be done with mod rewrite?
Thanks!
Quite frequently you use RewriteCond not for the condition it allows you to specify but for gaining access to and matching certain parts of a whole lot of server variables.
RewriteCond %{QUERY_STRING} id=([^&]+)
RewriteRule ^widgets/(.*)\.htm$ /solar.php?pg=$1&cat=widgets&id=%1
RewriteCond takes the QUERY_STRING and tries to match 'id=' followed by one or more characters that are not &. Those characters are stored %1.
You will need an additional RewriteRule (like the one in my previous post) to handle cases where there is no 'id=' in the query string since you made that a condition by specifying the RewriteCond.
Andreas