kmitnick.net@gmail.com Posted August 1, 2012 Share Posted August 1, 2012 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 Quote Link to comment Share on other sites More sharing options...
Christian F. Posted August 1, 2012 Share Posted August 1, 2012 Can you post the code where you're using this RegExp? Quote Link to comment Share on other sites More sharing options...
kmitnick.net@gmail.com Posted August 1, 2012 Author Share Posted August 1, 2012 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' ); Quote Link to comment Share on other sites More sharing options...
Christian F. Posted August 1, 2012 Share Posted August 1, 2012 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)(/(\..... Quote Link to comment Share on other sites More sharing options...
kmitnick.net@gmail.com Posted August 1, 2012 Author Share Posted August 1, 2012 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 Quote Link to comment Share on other sites More sharing options...
Christian F. Posted August 1, 2012 Share Posted August 1, 2012 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'); Quote Link to comment Share on other sites More sharing options...
kmitnick.net@gmail.com Posted August 1, 2012 Author Share Posted August 1, 2012 Thank you very much ChristianF, this solved my problem. I appreciate your concern. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted August 1, 2012 Share Posted August 1, 2012 You're welcome, glad I could help. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.