ultigma Posted August 12, 2013 Share Posted August 12, 2013 (edited) I'm having troubles getting this to work/understanding it. my httpd.conf is set to: "LoadModule rewrite_module modules/mod_rewrite.so LoadModule setenvif_module modules/mod_setenvif.so" Quick overview of the site: I have a category database | category_id | category | | 1 | dogs | | 2 | cats | and a navigation menu, Home | Dogs | Cats | the links are set to index.php?category=['category_id'] so [category_id] equals either 1 or 2 as home is explicitly declared What I am getting is localhost/testblog/index.php?category=1 but what i want the url to look like is localhost/testblog/dogs/ so that it uses the category_id to display posts from only that category. Here is my htaccess code: RewriteEngine On # Turn on the rewriting engine RewriteBase / RewriteRule ^dogs/$ index.php?category=1 [NC,L] RewriteRule ^cats/$ index.php?category=2 {NL,L] Is this COMPLETELY Wrong? Something worth mentioning, I DO NOT have a directory called dog or cat. I was trying to avoid using it. Is it a must to have a directory? Thanks for any help Edited August 12, 2013 by ultigma Quote Link to comment https://forums.phpfreaks.com/topic/281072-url-rewrite-cant-get-the-hang-of-it/ Share on other sites More sharing options...
requinix Posted August 12, 2013 Share Posted August 12, 2013 (edited) Try to avoid using RewriteBase - you generally don't need it anyways. The best way to do this is to make index.php accept the name of a category instead of the ID number. That way you can say "any directory that doesn't exist is going to be a category and should go through index.php". On that note, you should avoid polluting the root with fake directories: how about making it like "/category/dogs"? RewriteEngine on # Add a trailing slash RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^/?category/(\w+) $0/ [L,R] # Category RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^/?category/(\w+)/ index.php?category=$1 [L] Edited August 12, 2013 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/281072-url-rewrite-cant-get-the-hang-of-it/#findComment-1444566 Share on other sites More sharing options...
ultigma Posted August 12, 2013 Author Share Posted August 12, 2013 (edited) thanks for the reply. I can't seem to get my head round it atm. Because of that I want to just plow on to other things. Is there any structure I should follow to allow for easy integration when I am ready to try tackle .htaccess again? the blog will follow a simple structure such as 'website.com/animal-type/post-about-animal' where website.com/animal-type/ would display only the posts about that animal? Would "website.com?category=category_name&post=post_id" be the right way to implement "website.com/animal-type/post-about-animal/" in the future? Thanks Edited August 12, 2013 by ultigma Quote Link to comment https://forums.phpfreaks.com/topic/281072-url-rewrite-cant-get-the-hang-of-it/#findComment-1444608 Share on other sites More sharing options...
requinix Posted August 12, 2013 Share Posted August 12, 2013 Keeping in mind what I said about making your scripts work with names (since that's what you have in the URL) and not numbers (since you don't have that in the URL), Yes that'd be about right. Quote Link to comment https://forums.phpfreaks.com/topic/281072-url-rewrite-cant-get-the-hang-of-it/#findComment-1444614 Share on other sites More sharing options...
ultigma Posted August 12, 2013 Author Share Posted August 12, 2013 Thanks again Requinix! I'll use post_title perhaps as they'll all be unique =) Quote Link to comment https://forums.phpfreaks.com/topic/281072-url-rewrite-cant-get-the-hang-of-it/#findComment-1444621 Share on other sites More sharing options...
ultigma Posted August 14, 2013 Author Share Posted August 14, 2013 (edited) I've figured out how Mod rewrite works now however I've come across a new problem. htaccess code RewriteEngine On RewriteRule ^([A-Za-z0-9-]+)/$ http://localhost/testblog/index.php?category=$1 I now know that this: localhost/testblog/dogs/ is equal to : localhost/testblog/index.php?category=dogs however, when i put a link on the index.php file; <a href="dogs/"> Dogs </a> that page (localhost/testblog/dogs/) now has the stylesheet prefixed with dogs/ so the stylesheet looks like 'localhost/testblog/dogs/stylesheets/style.css' but when I link with ahref="index.php?category=dogs" Dogs </a> everything works just fine; 'localhost/testblog/stylesheets/style.css' I find it confusing because I thought that they now equaled the same thing using the .htaccess file? how can i rectify this problem with the prefix? Hopefully I've explained properly Thanks Edited August 14, 2013 by ultigma Quote Link to comment https://forums.phpfreaks.com/topic/281072-url-rewrite-cant-get-the-hang-of-it/#findComment-1444970 Share on other sites More sharing options...
Solution ultigma Posted August 14, 2013 Author Solution Share Posted August 14, 2013 (edited) Problem was I needed to explicitly declare the root url. Before: <link rel="stylesheet" href="stylesheets/style.css" type="text/css" /> After: <link rel="stylesheet" href="/testblog/stylesheets/style.css" type="text/css" /> by putting /testblog/ it solved the problems Edited August 14, 2013 by ultigma Quote Link to comment https://forums.phpfreaks.com/topic/281072-url-rewrite-cant-get-the-hang-of-it/#findComment-1444976 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.