I'm currently building out a simple site but struggling with a bit of htaccess.

I want my links such as services, about, blog etc to be categories.

I then want to have sub categories from then such as ppc for services, meet the team for about and a selection of blog posts for blog.

When you click on the link such as a post within ppc you get the following URL structure:



So far I have been able to get the url to show like this:




The two success examples above show that I can get it to show category name and subcategory name together.

Or I can get category-name and post-name together.

So i am struggling to get category-name/subcategory-name/post-name

My current htaccess file looks like this:

RewriteEngine On
RewriteBase /

RewriteRule ^services/(.*)$ catpost.php?id=$1 [L]
RewriteRule ^a-(.*)-(.*)$ archives.php?month=$1&year=$2 [L]

RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ viewpost.php?id=$1 [QSA,L]

I'm struggling with fixing these two issues:

1) Having a dynamic url insertion before the catpost.php

-You will see in the htaccess i have fixed the the catpost rewrite rule to have services before. However, is there a way to have that change dynamically?

So that could reference to services, about etc.

2) Keeping the subcategory-name in the url when you view the post as it directs to sitename/category-name/post-name

Any help is greatly appreciate. I hope this post is clear but if it isn't please let me know so I can improve.

1 Answers

David Ye On

Well, if I were you, I'll try to deal with all these in a index.php and refer to other pages by include().

Seems that (.*)/(.*) is not allowed as the FIRST rule. If you want to rewrite services/1 to services.php?id=1 and catpost/5 to catpost.php?id=5, try this:

RewriteRule ^a-(.*)-(.*)$ archives.php?month=$1&year=$2 [L]
RewriteRule ^([^/]+)/([^/]*)$ $1.php?id=$2 [NC,L]
  • [^/] stands for any char except a slash

To finish your secont task together, I tried:

RewriteRule ^([^/]+)/[^/]+/([^/]*)$ $1.php?id=$2 [NC,L]

(It doesn't look pretty. My fault.)

But it requires you to put something between the two slashes, like catpost/something/2, and seems that you'll get a 500 if you leave it empty.