【问题标题】:Wordpress URL not workingWordPress URL 不起作用
【发布时间】:2017-09-22 12:59:45
【问题描述】:

我正在使用 wordpress,我有一些 URL 问题。

我当前的 URL 是服务器上的 IP 地址:http://www.192.10.1.22/states/?q=ohio

我要网址:http://www.192.10.1.22/states/ohio

我在functions.php文件中使用了以下代码,它在我的 本地的,但是当我在 cpanel 中上传时,它现在正在工作,因为我出错了 找不到页面。

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

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

我也使用了下面的代码。

add_rewrite_tag('%states%', '([^&]+)');
global $wp;
    $wp->add_query_var( 'q' );
    add_rewrite_rule(
        'states/(\d*)$',
        'index.php/states?q=$matches[1]',
        'top'
    );

我也更新了永久链接,apache mode_rewrite 也开启了。

那么我该如何解决这个问题呢?

【问题讨论】:

    标签: php wordpress .htaccess mod-rewrite url-rewriting


    【解决方案1】:

    您可以直接在服务器上的 .htaccess 文件中添加规则。

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

    【讨论】:

    • 我直接在 htaccess 文件中使用了该代码,但给我的页面未找到。
    • 尝试添加 index.php: RewriteRule ^states/([^/]*)/? /index.php/states/?q=$1 [QSA,L]
    • 仍未找到。
    • 我更新了我的代码,尝试在 index.php 之后添加和不添加状态
    • 我按照你说的使用了,但现在它给了我 500 内部服务器错误。
    【解决方案2】:

    请使用以下代码。

    首先声明查询变量

    function custom_rewrite_rule() {
    global $wp;
    $wp->add_query_var( 'q' );
        add_rewrite_rule(
            'states/(/([^/]+))?(/([^/]+))?/?',
            'index.php?pagename=states&q=$1',
            'top'
        );
    }
    
    add_action('init', 'custom_rewrite_rule', 10, 0);
    

    【讨论】:

    • 这是我的荣幸。
    猜你喜欢
    • 2016-10-27
    • 2012-02-15
    • 2017-10-18
    • 1970-01-01
    • 1970-01-01
    • 2014-05-10
    • 2012-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多