mod_rewrite headache -- can you help?

texo

Expert Member
Joined
Jun 14, 2006
Messages
1,586
Reaction score
1
Location
Cape Town
I'm having a helluva time trying to understand this, and I'm really hoping that someone can help me with creating a rewrite rule to achieve the following:

I need the following:

http://DOMAIN.co.za/CATEGORY/page/2/

redirected to:

http://DOMAIN.co.za/CATEGORY&paged=2

(I know it seems strange, rewriting a clean url, but the clean url doesn't work with a wordpress plugin)

At the moment, I've got a rule which works great for the specific category, but I have no idea how to include a "wildcard" entry:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^CATEGORY/page/2/?$ http://DOMAIN.co.za/CATEGORY&paged=2 [R=301,L]

works great, but I wouldn't like to have to write one for every category on the site, and another for every page in that category...

help?
 
Don't know if it will work but you could try:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^CATEGORY/page/([0-9]*)/?$ CATEGORY&paged=$1
 
Thanks, ozymandias, but it doesn't work :(

(is there a way to have a wildcard for the CATEGORY bit, so that whichever category it is, the category is included as part fo the url string?)

Here's an example of an actual url, and what I'd like to have it rewritten as, if that will help:

http://mysite.co.za/issue-1/page/2/

should be rewritten as:

http://mysite.co.za/issue-1&paged=2

the "issue-1" bit will change as the category changes-- about 30 categories in total
 
Last edited:
(I know it seems strange, rewriting a clean url, but the clean url doesn't work with a wordpress plugin)

texo - I know nothing about Wordpress but from a Google search, it seems that the program has it's own mod_rewrite built-in and it's odd that it doesn't work for you. Is your (from the admin panel) setting for your permalinks (options -> permalinks) set on default? If it is, change it to one of the other options. You may be way ahead of me here so ignore this post if you've done all that.
 
I'm definitely not way ahead of you! This is the first time I've ever used WordPress.

I'm using custom permalinks:

/%category%/%postname%

which work great, with the resulting htaccess generated by Wordpress being:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

The problem now is that a plugin I'm using to generate "Digg-style" pagination is creating links to the next page in the form of

http://domain/category/page/2/

which don't work with my custom permalink setup... the plgin unfortunately doesn't have any options to change its output.

Thanks, anyway, ozy.
 
I'm definitely not way ahead of you! This is the first time I've ever used WordPress.

I'm using custom permalinks:

/%category%/%postname%

which work great, with the resulting htaccess generated by Wordpress being:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

The problem now is that a plugin I'm using to generate "Digg-style" pagination is creating links to the next page in the form of

http://domain/category/page/2/

which don't work with my custom permalink setup... the plgin unfortunately doesn't have any options to change its output.

Thanks, anyway, ozy.

Does that mean with the "Digg-style" pagination plugin, that you are not able to see of your pages or categories? That would definitely be a problem. Is this the case?
 
your rewrite rule should look something like this:

RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([0-9]+)/?$ $1&paged=$3
 
Does that mean with the "Digg-style" pagination plugin, that you are not able to see of your pages or categories? That would definitely be a problem. Is this the case?

That's right -- can only see the first page, the second page is whatever/page/2/ which 404s
 
your rewrite rule should look something like this:

RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([0-9]+)/?$ $1&paged=$3

Thanks BobbyMac, but that doesn't work...

domain.com/wildcard/page/2/

isn't being rewritten to

domain.com/wildcard&paged=2
 
texo - It's been quite a while since I did a mod_rewrite but I remember that I had to change some code in my PHP files. If links in my PHP files looked something like http://mysite.co.za/issue-$category&paged=$page_id then I would need to change that code to http://mysite.co.za/issue-$category/page/$page_id/ . The mod_rewrite rules in your .htaccess file would then convert your static URL back to the dynamic URL so that your parameters are passed to your PHP script in the format that PHP can understand.

Does your "Digg-style" pagination plugin create it's own .htaccess file or does it update the existing .htaccess file with new rules?
 
I double checked and it works on my test server:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([0-9]+)/?$ $1&paged=$3 [L]
 
texo: remember that you can't see the rewrite happening, it will just *happen*.
 
BobbyMac, I know that "it wiil just *happen*" :)

Here's the whole .htaccess file, inclduing the wordpress bit:
(I've tried adding your bit both before and after the wordpress bit, with and without the [L] -- which stops the rewriting, correct?)

RewriteEngine on
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([0-9]+)/?$ $1&paged=$3
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress
 
Thanks so much, BobbyMac -- I can see that it works like a charm there.
*scratches head*
must be something about the wordpress rewrite rules thats borking it.
 
The wordpress rule is basically taking the whole (and any) request and sending it back to index.php

RewriteRule . /index.php [L]
 
Thanks, BobbyMac -- it's clear you know far more than I ever will about mod_rewrite, so I've taken the easy way out and installed a wordpress "redirect" plugin which I can use to manually forward the pages.

I appreciate the time you've taken to help me out.
 
Top
Sign up to the MyBroadband newsletter
X