admin管理员组

文章数量:1122846

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?

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?

Share Improve this question edited Apr 26, 2017 at 11:23 Faysal Mahamud 1,0402 gold badges8 silver badges21 bronze badges asked Apr 26, 2017 at 5:05 user118443user118443 1
Add a comment  | 

2 Answers 2

Reset to default 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

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();

本文标签: htaccessWordpress URL not working