Rewrite Help

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

I am writing my own really simple blog script. I want the urls to be like

example.com/blog/categoryname/post-title

for the posts and for category views

example.com/blog/categoryname

My .htaccess file looks like this

RewriteEngine on
RewriteRule ^blog/([a-zA-Z0-9]+)$ /blog/$1/ [R]
RewriteRule ^blog/([a-zA-Z0-9]+)/$ /blog.php?category=$1

RewriteRule ^blog/([a-zA-Z0-9-]+)$ /blog/$1/$2/ [R]
RewriteRule ^blog/([a-zA-Z0-9-]+)/$ /blog.php?category=$1&post=$2
'

The category views work fine. But for some reason the post view isn't working. When I go to example.com/blog.php?category=other&post=why-i-blog I see the post like I'm supposed to. But when I go to /blog/other/why-i-blog I just get a 404. Any idea what I am doing wrong?

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

Geez, a few minutes after I post I find a solution

Options +FollowSymLinks
RewriteEngine on

RewriteRule blog/(.*)/$ /blog.php?category=$1
RewriteRule blog/(.*)/(.*)$ /blog.php?category=$1&post=$2
'

demonhale's picture

He has: 3,278 posts

Joined: May 2005

I was going to say the same thing, been figuring out the same thing last week... btw check your rss and trackback links, they might be affected with the rewrite... and don't forget to post a permanently moved error line on htaccess for all your old posts so that those tracking the links won't get a 404... if it's a new blog, no problem..

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

Can you do condidtional Rewrites?

ie. I want to use the following:

RewriteRule /(.*)$ /run/?mod=$1&cmd=view
RewriteRule /(.*)/(.*)$ /run/?mod=$1&cmd=$2
'
so that https://www.whatever.com/employees/edit woud take you to https://www.whatever.com/run/?mod=employees&cmd=edit

However, on our server we do have the need to directly browse to https://www.whatever.com/config as it is WITHOUT it rewriting it to https://www.whatever.com/run/?mod=config&cmd=view

The reason being is there are about 3 subdirectories off of the domain that are already backend utilities already in place.

Oh, and since I'm writing anyway, how would i handle a needed qs value anyhow.

Currenly for a few areas, I have a "non standard" value passed (every page has a "mod" and a "cmd"), but say I have the following:
https://www.whatever.com/run/?mod=employees&cmd=edit&gtype=i
Now, I know I could do
RewriteRule /(.*)/(.*)/(.*)$ /run/?mod=$1&cmd=$2&extra1=$3'
and then in the code for that module do $type=$_GET['extra1']

However, I'd rather be able to actually call it as
https://www.whatever.com/run/employee/edit/?gtype=i

Reason why are mainly becasue the the different mods that need the extra are as different variables, and one module can sometimes have 1 to 5 "extra" values.

Note that this is not really for SEO, just for personal tastes.

-Greg

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

Quote: Can you do condidtional Rewrites?

Um, I don't know for sure.

RewriteCond %REQUEST_URI != /config.php
RewriteRule /(.*)$ /run/?mod=$1&cmd=view

RewriteCond %REQUEST_URI != /config.php
RewriteRule /(.*)/(.*)$ /run/?mod=$1&cmd=$2

You might try that. I've never really used them though.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.