Issues with MOD_REWRITE

I am REALLY struggling with this book I bought on e-commerce. Things aren’t working as they are supposed to work. :frowning:

(The chapter I am in is a Product Catalog and he is trying to show how to use MOD_REWRITE to make your URL’s “prettier” and more SEO-friendly.)

The author says this should be displayed in the URL for the customer…


shop/coffee

but then he is rewriting it for the Search Engines like this…


shop.php?type=coffee

I don’t understand how this can work when all you see in the address bar is…


http://localhost/02_MyCoffeeShop/shop/coffee/

Another PROBLEM I am having is that when I click from Category to Category the URL is building off itself and not refreshing so I keep getting incorrect links like this…


http://localhost/02_MyCoffeeShop/shop/goodies/shop/coffee/

I have been stuck on this stupid chapter and example for a week now!! :frowning:

My goal is to finish the book and see how things work, and then I can go back and re-code things once I understand how they originally worked.

QUESTIONS:
1.) How do you turn on MOD_REWRITE?

I am working locally on my MacBook using NetBeans, and maybe the last problem I described is because the MOD_REWRITE code in my .htaccess file isn’t turned on/working?!

I created a .htaccess file and have it in my root development directory.

.htaccess file


[SIZE="1"]<IfModule mod_rewrite.c>
RewriteEngine on
# For sales:
RewriteRule ^shop/sales/?$ sales.php
# For the primary categories:
RewriteRule ^shop/([A-Za-z\\+]+)/?$ shop.php?type=$1
# For specific products:
RewriteRule ^browse/([A-Za-z\\+\\-]+)/([A-Za-z\\+\\-]+)/([0-9]+)$ browse.php?type=$1&category=$2&id=$3
# For HTTPS pages:
RewriteCond %{HTTPS} off
RewriteRule ^(checkout\\.php|billing\\.php|final\\.php|admin/(.*))$ https://%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
[/SIZE]

2.) If you make a URL “friendly” like this…


www.SomeSite.com/like/to/specific/product

then how does your browser know where to navigate??

Is the re-written URL hidden in the background?

Is that how a MOD_REWRITE works?

I could really use some help here. (I’d like to get this fixed tonight or Sunday at the latest.)

Thanks,

Debbie

DD,

Answers embedded:

Regards,

DK

DK,

When you response, can you please NOT use embedded comments.

They are hard to find within all of my comments and quotes and code.

And when I respond with quote, this is all I get… :wink:


http://localhost/02_MyCoffeeShop/shop/coffee/

The 02_MyCoffeeShop directory is problematic - but it depends upon where you’re locating your .htaccess file with the mod_rewrite.

Why is it problematic??

When I create a new “project” in NetBeans, it makes you set up a “Project Directory” which - I suppose - serves as your “web root”, although I’m not entirely sure.

All I know is that I treat my “Project Folder” like the web root because it is where I put “index.php” and all of my other files.

Here are the config settings in NetBeans for my project…


[b]Project Folder:[/b] 
/Users/user1/Documents/DEV/++htdocs/02_MyCoffeeShop

[b]Source Folder:[/b] 
/Users/user1/Documents/DEV/++htdocs/02_MyCoffeeShop

[b]Web Root:[/b] 
<Source Folder>

[b]Run As:[/b] 
Local Web Site (running as local web server)

[b]Project URL:[/b] 
http://localhost/02_MyCoffeeShop/

[b]Index file:[/b] 
index.php


http://localhost/02_MyCoffeeShop/index.php

My .htaccess file is in the “02_MyCoffeeShop” directory along with my index.php file.

I put it there because that is supposed to be my “web root” in Net Beans for this project. And you are supposed to put your .htaccess in the web root, correct?

[quote]Another PROBLEM I am having is that when I click from Category to Category the URL is building off itself and not refreshing so I keep getting incorrect links like this…

http://localhost/02_MyCoffeeShop/shop/goodies/shop/coffee/
That sounds like a combination of the directory and bad mod-rewrite code.[/quote]

Well, I explained my directory structure above. I have been using NetBeans for a couple years and that is always how you set things up, so that part is correct.

As far as how that relates to the .htaccess file, I don’t know since this is my first time using a .htaccess file.

I’ve accumulated YEARS of experience helping members with mod_rewrite into the tutorial linked in my signature. Your answer is there.

Except that both of your links don’t work… :wink:

Care to just help me out here? :slight_smile:

Have you installed Apache?

I’m using MAMP.

Have you UNCOMMENTED the mod_rewrite module?

It already was uncommented.

LoadModule rewrite_module modules/mod_rewrite.so

[quote]I created a .htaccess file and have it in my root development directory.

WHICH directory is that? Your localhost's DocumentRoot or the 02-whatever directory?

[/quote]

Correct. (See above)

As far as your comments on the contents of the .htaccess file, I appreciate them, but don’t think the “prettiness” of the author’s .htaccess code is the issue. (Of course I could be wrong.)

This appears to be a PATH issue…

Hope that answers your questions.

Now if someone can help me get this fixed asap today so I can get this dumb book done, get the author’s sample e-commerce site working, and then go back and build my own e-commerce site the right way!! :wink:

Thanks,

Debbie

The problem seems to be related to the fact that the directories on your local host and the directories on the website to be don’t match, i.e., on you local machine you have [noparse]http://localhost[/noparse]/02_MyCoffeeShop while on the live server it will just be [noparse]http://www.mycoffeeshop.com[/noparse]/, right?

This kind of thing can cause a lot of confusion and I would recommend you create a virtualhost on your local machine (for this you need to add a <VirtualHost> section to httpd.conf, I’m sure there are a gazilion tutorials out there how to do this with MAMP) and create an entry in the hosts file (see http://decoding.wordpress.com/2009/04/06/how-to-edit-the-hosts-file-in-mac-os-x-leopard/) so you can use something like [noparse]http://mycoffeeshop/[/noparse] locally.

Correct.

(That, and the lame author’s book I bought doesn’t bother to tell you this. Nor that his use of ‘/’ breaks the hell out of his code…)

This kind of thing can cause a lot of confusion and I would recommend you create a virtualhost on your local machine (for this you need to add a <VirtualHost> section to httpd.conf, I’m sure there are a gazilion tutorials out there how to do this with MAMP) and create an entry in the hosts file (see http://decoding.wordpress.com/2009/04/06/how-to-edit-the-hosts-file-in-mac-os-x-leopard/) so you can use something like [noparse]http://mycoffeeshop/[/noparse] locally.

I think I figured things out this weekend as far of this one point. (Caused me to lose 2+ weeks of my life over this asinine topic…)

When I have time, I’ll have to read up on “virtual hosts” to see if that make life easier the next time around.

Another issue in my original post that has not been addressed yet is this…

Another PROBLEM I am having is that when I click from Category to Category the URL is building off itself and not refreshing so I keep getting incorrect links like this…

http://localhost/02_MyCoffeeShop/shop/goodies/shop/coffee/

Now what is causing that?

I have that issue both in my Dev environment and in my Hosting environment. (Even after I fixed the path issue above.)

There are 2 categories: Coffee, Goodies

I can go from Home to Coffee and I can go from Home to Goodies, but if I go from category to category (i.e. links), the URL feeds off itself and you get…

http://localhost/02_MyCoffeeShop/shop/goodies/shop/coffee/

…along with a Page Not Found error.

That seem like an issue with the MOD_REWRITE file.

I can post my PHP or MOD_REWRITE here if you think it will help?!

Thanks,

Debbie

Sounds like you’re just using “shop/goodies” and “shop/coffee” in the HTML (or some scripting languages that generates HTML for that matter).
Thing is, once the current browser location is [COLOR=“Red”][noparse]http://www.example.com/shop/goodies[/noparse][/COLOR] and you click a link to “shop/coffee” it will direct you to [COLOR=“Red”][noparse]http://www.example.com/shop/goodies[/noparse][/COLOR]/[COLOR=“Blue”]shop/coffee[/COLOR]. “shop/coffee” being a relative URL and all that.

The easiest solution is to start every URL in the HTML with a /
So instead of “shop/goodies” and “shop/coffee” use “/shop/goodies” and “/shop/coffee” instead.
That will work the way you expect in that won’t keep adding stuff to the URL.

However, this will cause the problem that on you local machine it takes out the 02_MyCoffeeShop bit of the URL (dklynn told you that directory was problematic!)
You could solve this by either:

  1. put <base href=“/02_MyCoffeeShop”> in the <head> of the HTML on your local machine
  2. create a virtual host (and edit the hosts file) to create a local domain so you don’t have to use localhost/directory (see my previous post).

If you do either of those you can also use / perfectly on your local machine.