Forum Moderators: phranque

Message Too Old, No Replies

Basic mod rewrite help?

         

omghalp

12:36 am on Aug 2, 2011 (gmt 0)

10+ Year Member



Hey, I'm trying to set up some simple mod_rewrite to create friendly URLs. I've done this before but can't figure out for the life of me why this isn't working. I can enter the shortened URLs into the browser and get to the correct page, but the browser URL bar changes to show the dynamic URL for some reason. Here's the code:



RewriteEngine on


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (/|\.htm|\.php|\.html|/[^.]*)$ [NC]


RewriteRule ^product/([^/]+)$ http://www.mysite.com/index.php?sort=product&brand=$1 [L,R=301]
RewriteRule ^product/([^/]+)/$ http://www.mysite.com/index.php?sort=$1 [L,R=301]


If I visit "mysite.com/product/sony" it changes in the URL bar to "mysite.com/index.php?sort=product&brand=sony". Anyone know how I can keep the "mysite.com/product/" as the URL in the browser?

g1smd

2:20 am on Aug 2, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



With a domain name in the target, and the R=301 flag you have told the browser to redirect to a new URL.

What you actually need is an internal rewrite, by omitting both of those things from the rule.

lucy24

2:20 am on Aug 2, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I really hate this ### machine
I wish that they would sell it
It never does quite what I want
But only what I tell it.


Er. Sorry. But you can't fault the machine for redirecting you-- i.e. showing the new location in the address bar-- when you've explicitly asked it to do so. If you only want a rewrite-- no change to the address bar--leave off the http://www.example.com and the the R=301.

Oh, and you can express (htm|html) as simply (html?) meaning that the l is optional.

Your final condition and the first rule seem to be saying different things, suggesting that most of the condition can be eliminated.

The spacing of your example makes it look as if you expect the conditions to apply to both rules. I hope that's just typography.