Forum Moderators: phranque

Message Too Old, No Replies

Simple (Hopefully) Mod Rewrite

Mod_Rewrite

         

darrenalex

9:06 pm on Feb 2, 2011 (gmt 0)

10+ Year Member



Hello Everyone,
I am using pinnacle cart as our shopping cart software. The current cart pages is /index.php?p=cart and /index.php?p=checkout. I would like for it to /cart.html and /checkout.html. These are the only two I need so its not like I have a few hundred pages that would have to be manually coded.

Is this possible using Mod_Rewrite? I have found where if the page is /cart.php you can have it re-wrote to /cart.html but I have not been able to find what I need for this.

Thank you in advance for those respond and thank you webmaster world for this great site. I am sure I will be visiting often and hopefully be able to help others out more than I ask for it as this is becoming my full time job instead of a small part of it.

g1smd

9:54 pm on Feb 2, 2011 (gmt 0)

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



You need to change the links on the pages to point to the new URLs. It is those links that "define" URLs.

Once the links point to the correct URLs, users clicking those links will make a request for the right URL.

That's where your rewrite comes in. It accepts the incoming URL request, and "translates" it to get the content from the right place inside the server, without revealing where inside the server that content really resides.

darrenalex

10:07 pm on Feb 2, 2011 (gmt 0)

10+ Year Member



Thank you for the quick response. So I need to get the links updated to cart.html and then I need to add to the rewrite. Would the rewrite script be similar to?

RewriteRule ^(.*)\.php$ $1.html [R=permanent]

Thanks,
Darren

g1smd

11:00 pm on Feb 2, 2011 (gmt 0)

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



That code is for a redirect, not a rewrite.

The redirect code is used to tell agents asking for the old URL that it now exists at a new URL. That's the other part of the process - forcing searchengines to update their index to use the new URL.

Your external redirect code will need a RewriteCond testing THE_REQUEST to see if
example.com/index.php?p=cart
or
example.com/?p=cart
has been requested by an external client, and if it has, to redirect to
example.com/cart.html
. This rule will have the
[R=301,L]
flags and the redirect target will contain the protocol and domain name, as well as the path the user is being redirected to.

You also need an internal rewrite such that when the URL
example.com/cart.html
is requested, the file at
/index.php?p=cart
is used to serve the content. This rule needs only the
[L]
flag, as it is for an internal rewrite.

From this it is apparent that you need to exactly clear on the differences between redirects and rewrites and to know that URLs used out on the web, and filepaths and filenames used inside the server are not at all the same thing. They are merely "related" by the action of the server.

In order to simplify the code needed, I would also urge you to drop the
.html
extension from your rewritten URLs. This makes the pattern matching much easier. You can then rewrite all "extensionless" URL requests to your script in one rule, without having to have multiple rules, one for each URL.

jdMorgan

8:08 pm on Feb 7, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




# Internally rewrite client requests for /cart.html and /checkout\.html
# URL-paths to index.php script filepath with query
RewriteRule /(cart|checkout)\.html$ /index\.php?p=$1 [QSA,L]

Jim