skip to Main Content

I’m working on a simple five page website which includes a blog. This is the first time I’m using .htaccess to correctly rewrite my URLs for SEO friendly optimization.

The two parameters I am trying to allow a visitor to use is sitename.com/blog/category/WORD or sitename.com/blog/SLUG. If /category/word is used, I’d want it to list all the posts with the chosen category word. If /slug is used (short title of a post such as “learn-how-to-code”), I’d want it to just show that specific post.

Here’s my old .htaccess:

RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteCond %{REQUEST_URI} !^/[0-9]+..+.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/.well-known/pki-validation/[A-F0-9]{32}.txt(?: Comodo DCV)?$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.php [NC,L]

It was originally working fine for single pages, such as sitename.com/about, sitename.com/contact and so on, but any time I tried sitename.com/blog/SLUG or sitename.com/blog/category/WORD it would just redirect to the blog page. I am echoing $_GET[‘category’] and $_GET[‘slug’] in blog.php to test it, but it never showed anything.

After looking at a few posts here, I figured out that I needed to add some separate lines to just check on /blog being used and to pull in the appropriate parameters.

Here’s my current .htaccess:

RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteCond %{REQUEST_URI} !^/[0-9]+..+.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/.well-known/pki-validation/[A-F0-9]{32}.txt(?: Comodo DCV)?$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^blog/([^.]+)$ blog.php?slug=$1 [NC,L]
RewriteRule ^blog/category/([^.]+)$ blog.php?category=$1 [NC,L]
RewriteRule ^([^.]+)$ $1.php [NC,L]

The sitename.com/blog/SLUG is working fine, but anytime I do sitename.com/blog/category/WORD, it’s showing “category/WORD” as the slug, and not breaking it into the category word.

Am I doing something wrong here? Is there a better way of doing this?

2

Answers


  1. You’d need to swap your ^blog lines. Since the first one is using the L flag, you’re essentially telling it to stop using other rules, so it’s already picking up $1 as category/WORD in your case.

    I don’t know how the rest of your backend is setup, but you could also look into routing everything through an index page.

    Login or Signup to reply.
  2. Simple explanation without tech is for better clarification

    Line1 RewriteRule ^blog/([^.]+)$ blog.php?slug=$1 [NC,L]
    Line2 RewriteRule ^blog/category/([^.]+)$ blog.php?category=$1 [NC,L]

    in line 1 you are doing regular expression checking by code ([^.]+) after the url blog
    and in line 2 your url is kind of hard coded part of category and then you are doing regular expression check

    so in simple work regular engine never goes to the line2 of code because it already become part of line1 is already checked and passed regular expression part so category becomes part

    simple tip: Always keep hard url parts first and regular expression parts rules at the end.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search