Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite question

         

rufous

4:39 am on Feb 22, 2004 (gmt 0)

10+ Year Member



Question for you mod_rewrite pros:

I have a site for selling my used books using php/mysql and have started learning mod_rewrite on it. I have a list of category links in the format of books/category.html which rewrite to catalog.php?category=category. This part works fine.
When I click on one of these links, a list of books in the category comes up, with links to the books (also rewritten) and the category list remains also. Herein lies the problem. The category links are now in the format of books/books/category.html in the status bar when I hover the mouse over the link, but when I view source the link is still "books/category.html", however, the link still tries to go to books/books/category.html. If I click on a category again, I now have books/books/books/ etc. but the view source still only shows books/category.html. So the question is, where is the extra "books" coming from?

RewriteEngine on
RewriteBase /
RewriteRule ^books/(.+)\.html$ catalog.php?category=$1 [L]

Thanks in advance,

Jim

jdMorgan

5:03 am on Feb 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



rufous,

Welcome to WebmasterWorld [webmasterworld.com]!

A link of the form: <a href="books/category.html"> means, "Take the current (sub)directory name, and add 'books/category.html' to it."

Therefore, if the current subdirectory name is "books/", then the result will be "books/books/category.html"

This gets confusing and complicated when using a script, but it may be that you simply need to specify your links in the form: <a href="/books/category.html"> -- That is, still in relative-link form, but specified as starting at your root directory. In other words, provide the path from your "home page" directory on down, starting with a slash. This way, you won't need to link to "http://www.example.com/buy/books/category.html", but rather only "/buy/books/category.html".

This may not in fact be your problem. But remember that the browser is responsible for resolving relative links, and so it will - but based on where it thinks it is getting files from, and not where they really are on your server. As you know, mod_rewrite "disconnects" URL-pathnames from resource filenames, because you can rewrite/redirect them at will. Similarly, using a script to provide dynamic content "disconnects" URLs from *any* real resource filename; All resource content is provided by the script.

This can leave the browser (and the webmaster) confused and confounded (it makes my head hurt), and that is the case here.

The key is to remember that the browser must resolve anything except a canonical (full) URL. That may not give you the answer instantly, but it sure helps untangle things a bit.

Jim

jdMorgan

5:09 am on Feb 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<more>
To further nail things down, you might also want to do the same thing to the script pathname -- "Root it" to your home directory:

RewriteEngine on
RewriteBase /
RewriteRule ^books/(.+)\.html$ [b]/buy/[/b]catalog.php?category=$1 [L]

Note: In both posts above, I have invented the "buy/" subdirectory as an example subdirectory. You may have no such subdirectory, or you may have a much longer path that needs to go in there. It all depends on where the files are located - or appear to be located - relative to your home directory.
</more>

Jim

rufous

2:19 pm on Feb 22, 2004 (gmt 0)

10+ Year Member



Thanks JD!

Yes, it was as simple as adding / to the beginning of the path. I appreciate the help and explanation.

Jim