0

I am using Wordpress, I have some URL issue.

My current URL is IP address on server: http://www.192.10.1.22/states/?q=ohio

I want URL: http://www.192.10.1.22/states/ohio

I used following code in functions.php file and it's working in my local but when I upload in Cpanel then it's now working given me an error page not found.

function custom_rewrite_rule() {
      add_rewrite_rule(        
            'states/([^/]*)/?',        
            'index.php/states/?q=$1',        
            'top' );
    }

    add_action('init', 'custom_rewrite_rule', 10, 0);

I also update permalink and apache mode_rewrite is also on. So how could I solve this issue?

2 Answers 2

0

This is not how rewrite_rules works in Wordpress. You miss two things.

  1. Match. It has $1, but in reality (take a look to example), you need to work with $matches.
  2. Your Query Var. You using q, but its not wp native or declared in example code. You can use one of the public ones or declare your query var and logic behind it.
  3. If you are OK with Regular Expressions and already implemnt first two changes, but it still doesn't work - I would suggest to use debug bar and my plugin debug bar rewrite rules
0

Add the following code in the functions.php

add_filter('query_vars', 'add_state_var', 0, 1);
function add_state_var($vars){
    $vars[] = 'state_names';
    return $vars;
}

function custom_rewrite_rule() {
      add_rewrite_rule('^states/([^/]*)/?','index.php?post_type=page&pagename=states&state_names=$matches[1]','top');
}

add_action('init', 'custom_rewrite_rule', 10, 0);
flush_rewrite_rules();
3
  • This will bring to running flush_rewrite_rules()every single request. Commented Apr 27, 2017 at 7:55
  • Once run flush_rewrite_rules just comment that line. Commented Apr 27, 2017 at 12:06
  • Should you add this to code or something? Commented Apr 27, 2017 at 13:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.