Plummer
Plummer

Reputation: 6698

wordpress url rewrite rules producing 404 error

I'm working with a theme where there seems to be a 404 error with a rewrite rule. The rule is as follows:

products/([^/]+)/page/?([0-9]{1,})/?$

index.php?portfolio=$matches[1]&paged=$matches[2]

First page looks and displays fine, but the url request for the pagination doesn't look right:

http://www.site.com/products/page/2

Where I assume, from the rewrite rule, it should look like:

http://www.site.com/products/foo/page/2

So I'm solving for foo. It seems to be a missing level/string for ([^/]+), which I assume it's some sort of PCRE/preg_match, as noted here, which I know very little about. From what I can tell it means:

start subpattern > start character class definition > assert start of subject (or line, in multiline mode) > No clue > end character class definition > 1 or more quantifier > end subpattern

UPDATE:

This is associated with a theme not of my design. I used Rewrite Rules Inspector to find the rewrite rule inquestion.

The problem is with a pagination link with a gallery shortcode. The shortcode works on the site's main page, but when it's called on a different page (ie, /products page), and then you try to access the next page, you get a 404. It works when permalinks are set to default. Which is why I assumed the problem is with a rewrite rule.

SOLUTION:

After digging through the plugin and revising myself on rewrite behavior, I realized the plugin was not creating a rewrite rule to handle the pagination for the portfolio page. So, I made one myself and implemented it with as an mu-plugin and then emailed the plugin author with issue.

add_action( 'init', 'addMyRules' );

function addMyRules(){
    add_rewrite_rule('(.+?)/page/?([0-9]{1,})/?$','index.php?pagename=$matches[1]&paged=$matches[2]','top');
}

Upvotes: 1

Views: 3130

Answers (1)

Michael Lewis
Michael Lewis

Reputation: 4302

Here are a few thoughts:

I would use this WordPress rewrite analyzer plugin to look at which rules are being matched. It says its outdated, however the plugin developer (Jan Fabry) is very talented. It was still working a few months ago, last time I used it.

WordPress keeps an array of rewrite rules (I forget where to find it, probably on a $wp_rewrite global or something). The plugin above helps you match against these.

How are you generating your pagination links? Your problem may be that your pagination links are being generated improperly, rather than the rewrite rule not working? In other words, if you're using a paginate_links() function to generate these links, they might not be generating them to match your rewrite rule. Try typing your */foo/* url directly.

Also, remember that you need to refresh your rewrite rules whenever you make changes to them. You can do this by visiting the Settings > Permalinks page in the /wp-admin/ backend (you might have to submit the form on this page to resave your permalink settings, although I've heard that this isn't necessary).

As far as I know, the rewrite analyzer should be able to accurately match your urls to the rewrite rules that you've created. Just make sure to flush the rewrite rules whenever you make changes.

RE: Update

You have 2 options: Make your rewrite rule match your existing gallery pagination links, or make the gallery pagination links match your existing rewrite rule.

To do the latter, you'll want to find the gallery shortcode. Do a search through the wp-content folder for add_shortcode( function, and you'll find where the gallery shortcode is being defined. Then, go in and change the paginate_links() function. It takes various arguments that affect how the links are formatted.

Upvotes: 1

Related Questions