Forum Moderators: phranque

Message Too Old, No Replies

.htaccess modification

Not too sure how to do this?

         

phill2000star

12:35 am on Nov 15, 2007 (gmt 0)

10+ Year Member



Hi all.

I posted on here a few months back and got some great help getting my .htaccess file finally working. And now I want to make some additions to it.

Here it is at the moment.
=======================


Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([^.]+)$ ../view_products.php?product_id=$1 [L]

=======================

This is quite a simple one really and simply takes http://www.example.com/products/12345 and redirects to http://www.example.com/view_products.php?product_id=12345

but what I want is to change it so that it redirects http://www.example.com/products/12345.html and redirects to http://www.example.com/view_products.php?product_id=12345

Does anyone have any suggestions because I can find out how to change file extensions, but can't figure out how to use the filename on its own (minus the extension of .html or .php) and use that for the URL variable in the GET string of the page its redirecting to?

In the future I was thinking of adding to this .htaccess file to do the following.

http://www.example.com/products/product_category to http://www.example.com/view_categorys.php?category=product_name.

Now this will be residing in the same folder as the previous request. Will I be able to both of these requests together?

Many thanks!

[edited by: jdMorgan at 1:12 am (utc) on Nov. 15, 2007]
[edit reason] example.com [/edit]

jdMorgan

1:14 am on Nov 15, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> but what I want is to change it so that it redirects http://www.example.com/products/12345.html and redirects to http://www.example.com/view_products.php?product_id=12345

For this specific case, there are several ways to do it. Here are two, differing in how specific they are:


RewriteRule ^([^.]+)\.html$ ../view_products.php?product_id=$1 [L]
-or-
RewriteRule ^([0-9]+)\.html$ ../view_products.php?product_id=$1 [L]

The first is more general, requiring only an .html extension, while the second also requires a numeric-only id number.

These are not external "redirects," by the way. They are internal server filepath rewrites. The difference is non-trivial.

You can read up on back-references in the Apache mod_rewrite documentation, and on regular expressions in the document
cited in our forum charter [webmasterworld.com]

I'm not sure how to answer the concern expressed in your second question. You can add hundreds of RewriteRules if you like. As long as the originally-requested URLs are unique in some discernible way, only one of the rules will be invoked.

Jim

phill2000star

1:33 am on Nov 15, 2007 (gmt 0)

10+ Year Member



Hi JDMorgan,

that was just what I was looking for.

Superstar!