Forum

Thread tagged as: Question, Problem, Addons

Help With Rewrite Rule

Currently I have category links in a sidebar for my blog to enable viewing only posts for that category.

The code for the link is

<a href="?cat=<perch:category id="catSlug" />"><perch:category id="catTitle" /></a>

Which displays as /news/?cat=my_blog_category, I would rather it showed as /news/my_blog_category

So I tried changing this in htaccess:

RewriteRule ^news/([a-zA-Z0-9-/]+)$ /news/?cat=$1 [L]

and the link to:

<a href="<perch:category id="catSlug" />"><perch:category id="catTitle" /></a>

But it does not work, anyone know how to do this? I know the rewrite is working as I have clean urls for the posts.

Nigel Coath

Nigel Coath 1 points

  • 7 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

my_blog_category contains underscores, but your regexp pattern doesn't include any.

That was just a dummy category for example purposes!

So it might be /news/?cat=local-news

Drew McLellan

Drew McLellan 2638 points
Perch Support

RewriteRule ^news/([a-zA-Z0-9-/]+)$ /news/?cat=$1 [L]

should match

/news/local-news

Thats what I had to start with above, but it does not load the page with any perch articles.

/news/?cat=local-news works but the rewrite does not, any idea why its not working?

This is my whole news rewrite rule:

RewriteRule ^news/([a-zA-Z0-9-/]+)$ /news/post.php?s=$1 [L]
RewriteRule ^news/([a-zA-Z0-9-/]+)/preview$ /news/post.php?s=$1&preview=all [L]
RewriteRule ^news/([a-zA-Z0-9-/]+)$ /news/?cat=$1 [L]

The post rules work okay.

Drew McLellan

Drew McLellan 2638 points
Perch Support

You have the same pattern for the posts.

So should I change it to

RewriteRule ^news/category/([a-zA-Z0-9-/]+)$ /news/?cat=$1

and the link to

<a href="/category/<perch:category id="catSlug" />"><perch:category id="catTitle" /></a>
Drew McLellan

Drew McLellan 2638 points
Perch Support

That would help.

Tried it and if I delete these it works:

RewriteRule ^news/([a-zA-Z0-9-/]+)$ /news/post.php?s=$1 [L]
RewriteRule ^news/([a-zA-Z0-9-/]+)/preview$ /news/post.php?s=$1&preview=all [L]

But with these rules in it does not, any idea why?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes! You've got two patterns that match the same thing. Only the first will ever match.

But surely

RewriteRule ^news/([a-zA-Z0-9-/]+)$ /news/post.php?s=$1 [L]

Is a different pattern from

RewriteRule ^news/category/([a-zA-Z0-9-/]+)$ /news/?cat=$1

If not what is the pattern?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, it is now. Put the most specific first. The former also matches the latter.

This really is beyond the scope of Perch support.

Thanks!! The order solved it, putting the category one first made the difference.

PS I realise its beyond your support and I was not necessarily expecting your help, was hoping someone else might have the answer. I spent ages trawling mod_rewrite docs but it was just the order that stopped it.