Jump to content

URL Rewrite - Can't get the hang of it.


ultigma

Recommended Posts

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 

Link to comment
https://forums.phpfreaks.com/topic/281072-url-rewrite-cant-get-the-hang-of-it/
Share on other sites

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]

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.