Rewriting urls with mod rewrite

I want to turn [noparse]http://www.mysite.com/browse.php?cat_name=dresses&prod_brand=debenhams&discount=30&size=8&currentpage=1[/noparse]

into [noparse]http://www.mysite.com/browse/dresses/debenhams/30/8/1[/noparse]

Prod_brand,size, discount and currentpage are optional, as they’re not always present in the url.

I’ve got the following code in my htaccess file:

Options +FollowSymlinks
RewriteEngine on

RewriteRule ^browse/(\w+)/(\w+)/$ /browse.php?cat_name=$1&prod_brand=$2 [nc]

So far the code in the htaccess file hasn’t worked, am I doing something wrong? Do i need to change all the links manually in my code from

[noparse]http://www.mysite.com/browse.php?cat_name=dresses&prod_brand=debenhams&discount=30&size=8&currentpage=1[/noparse]

to [noparse]http://www.mysite.com/browse/dresses/debenhams/30/8/1[/noparse],

or this that unncesessary?

my mod rewrite is definitely enabled and working properly, I’ve tested it.

Thanks in advance

Where did the discount, size and currentpage go? Shouldn’t they be in there as well?

Also, you do not need the [NC] flag there, but you do need the [L] flag.
The [NC] flag is needed if you want to match case insensitive, but since it is you who decided what the URLs should look like, and you decided to go for /browse, there is no need to match any other case variants of that (and it will in fact slow Apache down trying).
The [L] flag is to indicate Apache it should go on processing other RewriteRules if this one matches; it’s similar to ; in PHP

Yes, you’ll have to change all the links manually. Alternatively you could take a look at the section “Redirect TO New Format” in this article: http://www.datakoncepts.com/seo
Which is a good idea anyway because it will also indicate to search engines that your URLs have changed, so they can take that into account.

Thanks. You’re right, size, discount and currentpage should be in the url.

I’ve changed one link in my code (just to test), it looks like this:

www.mysite.com/browse/$cat_name/$prod_brand

My htaccess is still the same as before, except I’ve replaced the nc tag with the L tag as per your explanation:

Options +FollowSymlinks
RewriteEngine on

RewriteRule ^browse/(\w+)/(\w+)/$ /browse.php?cat_name=$1&prod_brand=$2 [L]

However, when i click on my test link it says page not found. Is there something I’m missing?

See what I mean? :slight_smile:

Sorry not quite sure what you mean, could you explain?

If you mean I need to get rid of the slash before the $ sign, I have done, and this is what the htaccess looks like now:

RewriteRule ^browse/(\w+)/(\w+)$ /browse.php?cat_name=$1&prod_brand=$2 [L]

my url is browse/$cat_name/$prod_brand

For some reason, when I click on the link, I get a “not found” message “The requested URL /browse.php was not found on this server.”

What am i doing wrong?

Thanks in advance

Are you sure browse.php exists?

If you go to /browse.php?cat_name=something&prod_brand=something-else it does work?

Yes browse.php does exist and if you go to /browse.php?cat_name=something&prod_brand=something-else it takes you to the right page, but if you go
browse/$cat_name/$prod_brand it tells you browse.php doesn’t exist…

Hm, the fake directory you’re creating with the RewriteRule is also called “browse”.

Try replacing


Options +FollowSymlinks

with


Options +FollowSymlinks -MultiViews

If that doesn’t work, try to replace


RewriteRule ^browse/(\\w+)/(\\w+)$ /browse.php?cat_name=$1&prod_brand=$2 [L]

with


RewriteRule ^browse/(\\w+)/(\\w+)$ browse.php?cat_name=$1&prod_brand=$2 [L]

(remove the / before browse.php)

If that doesn’t work I’m out of idea’s I’m afraid …

Thank you, removing the / before browse/php worked, it now takes me to the right page. But it looks like it’s not finding the css because the page is not styled at all and also is missing all the image content (these come from a database). Any idea why this is happening?

I do. Check out the section “Relative Links Are Missing!” in this article :slight_smile:

Thank you, I will check it out.