Forum Moderators: phranque
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
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
RewriteEngine on
RewriteBase /
RewriteRule ^books/(.+)\.html$ [b]/buy/[/b]catalog.php?category=$1 [L]
Jim