Forum Moderators: phranque

Message Too Old, No Replies

htaccess Rewrite Syntax Help

         

aboutwd

10:48 pm on Dec 2, 2010 (gmt 0)

10+ Year Member



I'm trying to turn this...
https://www.domain.com/product_info.php/products_id/287


into this...
http://domain.com/index.php?product=287


...using htaccess.

But I can't get it to work. I can't even get close.
Can anyone help?

I need both http and https to work as well as WITH www. and WITHOUt www.

[edited by: aboutwd at 11:22 pm (utc) on Dec 2, 2010]

g1smd

11:05 pm on Dec 2, 2010 (gmt 0)

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



What have you tried so far?

There are literally ten thousand prior threads discussing this issue, very many of which have example code.

aboutwd

11:19 pm on Dec 2, 2010 (gmt 0)

10+ Year Member



OK, sorry about repeating the question then.

I read through a ton of them and, being very new to apache, I can't make sense of how the structure works well enough to adapt them to my situation.

Here was my latest attempt. (didn't work - of course)

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)(.*)$ /$3?$1=$2 [N,QSA]

jdMorgan

11:42 pm on Dec 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> I read through a ton of them.

Did you read the mod_rewrite documentation at apache.org? That's the best way to get started, or at least to focus your questions... Links to those docs and to other useful resources can be found in our Apache Forum Charter. Examples can be found in our Library. Links to the Charter and the Library are at the top of this page.

Assuming that you wish to rewrite requests for the URL-path /product_info.php/products_id/287 to the script at the server filepath /index.php?product=287, then all that is needed is something like:

RewriteRule ^product_info\.php/products_id/([0-9]+)$ /index.php?product=$1 [L]

I assume that you intend to put this code into domain.com/.htaccess. If not, please let us know.

This further assumes that you already have other working rewriterules. If not, you will need either both of the following lines or only the second one, placed at the top of your RewriteRules. You may either need the first line to prevent your server from throwing an error, or the presence of the first line itself will throw an error. This depends on your current server configuration, and the easiest way to find out is simply to test it with and without the first line.

Options +FollowSymLinks -MultiViews
RewriteEngine on

Note that there is really no reason to use "/product_info.php/" in your URLs -- Using just "/info/" or "/products/" would work just as well, and be a bit shorter and a bit more memorable as well. You don't actually need to have anything in that "virtual directory-level" of the URL at all, unless there are other "products_id/<number>" URLs in a different "virtual directory-path" which should NOT get rewritten.

Since you are apparently making a URL-format change now, consider making that change as efficient/effective as possible.

Jim

aboutwd

12:03 am on Dec 3, 2010 (gmt 0)

10+ Year Member



Thanks for your help... that was exactly what I needed.

Actually, I'm not constructing links with "product_info.php" in them. This is a shopping cart for authors, so we have hundreds of incoming links in that format.

We just migrated from OSCommerce to Opencart and this redirect code allows the existing authors' links to continue working in this new cart environment.

Thanks so much for your help. You made my day.
(plus, I'll visit the docs you suggested and read more about mod_rewrite. I might even learn something.)

g1smd

8:23 am on Dec 3, 2010 (gmt 0)

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



Assuming that you wish to rewrite requests for the URL-path /product_info.php/products_id/287 to the script at the server filepath /index.php?product=287, then all that is needed is something like:

The code above is for a rewrite, so does not match your goal.

You instead need a redirect, and it is simple to do.

In the the code above add the protocol and domain name to the target and add the [R=301,L] flags.