Jump to content

url regex not working in wordpress


kmitnick.net@gmail.com

Recommended Posts

Hi,

I know this is not a wordpress (wp) forum, but I'm using php regular expressions and I need help.

My site is basically a wp plugin that adds a specific search page, so I'm working on providing more SEO friendly urls.

I'm trying to match an url like this:

http://mysite.com/2-bedrooms/3-bathrooms/mycity/2.html

that means the user is looking for a house with 2 beds, 3 baths, placed on mycity and it is requesting the page 2.

 

The regex I'm using is:

http://mysite.com/(/(\w+)-bedrooms)*(/(\w+)-bathrooms)*(/(\w+)-city)*/\b([^.]+)\b/(\d+).html

 

but it's not working at all. I'm able to match the ulr withour the page, but when I add the "/2.html" for page 2, wp doesn't understand it and I get a error 404: page not found.

 

All I need is a regex to match with the above given url.

Please, give me some hints.

Thanks in advance

 

Link to comment
https://forums.phpfreaks.com/topic/266549-url-regex-not-working-in-wordpress/
Share on other sites

Hi, thanks for the replay, here is the code I'm using:

 

function insert_rewrite_rules( $rules ) {

$newrules = array();

$page_name = 'mypage';

 

        //basic rule

$newrules['(' . $page_name . ')$'] = 'index.php?pagename=$matches[1]';

 

//search by id

$newrules['(' . $page_name . ')/(\d+)$'] = 'index.php?pagename=$matches[1]&id=$matches[2]&search=1';

 

//url with page included

$newrules[(mypage)(/(\w+)-bedrooms)*(/(\w+)-bathrooms)*/([^.]+)/(\d+).html$'] = 'index.php?pagename=$matches[1]&bedrooms=$matches[3]&bathrooms=$matches[5]&query=$matches[6]&page=$matches[7]';

 

        //url with no page

$newrules[(mypage)(/(\w+)-bedrooms)*(/(\w+)-bathrooms)*(/([^.]+))*$'] = 'index.php?pagename=$matches[1]&bedrooms=$matches[3]&bathrooms=$matches[5]&query=$matches[7]';

return $newrules + $rules;

}

add_filter( 'rewrite_rules_array','insert_rewrite_rules' );

Please use [code][/code] or [php][/php] tags when posting code, as it makes it a whole lot easier to actually read both the post and the code.

 

That said: You have a syntax error on line 12, which you should fix.

Line 12: $newrules[(mypage)(/(\.....

Hey, the error must be at copy & paste time, my bad, I was trying to paste only what's important to avoid confusion.

Here is the fixed code:

 

<code>

function insert_rewrite_rules( $rules ) {

  $newrules = array();

  $page_name = 'mypage';

 

        //basic rule

  $newrules['(' . $page_name . ')$'] = 'index.php?pagename=$matches[1]';

 

  //search by id

  $newrules['(' . $page_name . ')/(\d+)$'] = 'index.php?pagename=$matches[1]&id=$matches[2]&search=1';

 

  //url with page included

  $newrules['(' . $page_name . ')(/(\w+)-bedrooms)*(/(\w+)-bathrooms)*/([^.]+)/(\d+).html$'] = 'index.php?pagename=$matches[1]&bedrooms=$matches[3]&bathrooms=$matches[5]&query=$matches[6]&pagem=$matches[7]';

 

        //url with no page

$newrules['(' . $page_name . ')(/(\w+)-bedrooms)*(/(\w+)-bathrooms)*(/([^.]+))*$'] = 'index.php?pagename=$matches[1]&bedrooms=$matches[3]&bathrooms=$matches[5]&query=$matches[7]';

  return $newrules + $rules;

}

add_filter( 'rewrite_rules_array','insert_rewrite_rules' );

</code>

 

I figured it out that wordpress interprets the &page=2 querystring and redirects to "mysite.com/2" url automatically, so I changed the "page" param to "pagem", only to avoid wp redirection.

Anyway, I need the regex to catch the number of the page the user is requesting.

Thanks again

Your problem is the use of [^.], which translates to "not a character" (or "only newlines", depending upon the modifier used).

 

I've corrected the RegExps, plus removed some unnecessary capturing and make things a bit cleaner.

<?php

function insert_rewrite_rules ($rules) {
$newrules = array ();
$page_name = 'mypage';

//basic rule
$newrules['(' . $page_name . ')$'] = 'index.php?pagename=$matches[1]';

//search by id
$newrules['(' . $page_name . ')/(\d+)$'] = 'index.php?pagename=$matches[1]&id=$matches[2]&search=1';

//url with page included
$newrules['(' . $page_name . ')(?:/(\\w+)-bedrooms)?(?:/(\\w+)-bathrooms)?/([^/]+)/(\\d+).html$'] = 'index.php?pagename=$matches[1]&bedrooms=$matches[2]&bathrooms=$matches[3]&query=$matches[4]&pagem=$matches[5]';

//url with no page
$newrules['(' . $page_name . ')(?:/(\\w+)-bedrooms)?(?:/(\\w+)-bathrooms)?(?:/([^/]+))*$'] = 'index.php?pagename=$matches[1]&bedrooms=$matches[2]&bathrooms=$matches[3]&query=$matches[4]';
return $newrules + $rules;
}

add_filter ('rewrite_rules_array', 'insert_rewrite_rules');

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.