【问题标题】:Wordpress rewrite issue - 404Wordpress 重写问题 - 404
【发布时间】:2013-09-16 08:41:30
【问题描述】:

我在 wordpress 尝试重写简单的 url 模式以满足我的需求时遇到问题。

正在寻找:

How can I rewrite: localhost/projects/55/1 into index.php?page_id=X&proID=55&subID=1 without getting a 404?

到目前为止我尝试了什么:

直接修改.htaccess:不起作用。然后我阅读了一些示例并通过在 init-hook 上调用 wordpress 函数 add_rewrite_rule() 进行了尝试。这是我当前的代码:

add_action('init', 'add_rewrite_rules');
function add_rewrite_rules() {
    add_rewrite_rule('^projects/([^/]*)/?([^/]*)?/?','index.php?page_id=8&proID=$matches[1]&subID=$matches[2]','top');
}

有人可以帮我解决这个问题吗?

编辑 1: Reference

编辑 2:

好的,更好。现在我不再得到404了。但遗憾的是,它仍然无法正常工作(也许我做错了什么)。

echo '<pre>'; var_dump($_REQUEST); var_dump(get_query_var('proID')); var_dump(get_query_var('subID')); echo '</pre>';

结果:

array(0) {
}
string(0) ""
string(0) ""

有一个页面重新加载,所以它指向localhost/projects

萤火虫: URL: http://10.0.0.109:8888/projects/25/2 Status: 301 Moved Permanently Domain: 10.0.0.109:8888 Size: 3.1 KB RemoteIP: 10.0.0.109:8888

htaccess 文件:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    
    # Custom rule
    RewriteRule ^projects/([0-9]+)/?([0-9]+)?/?$ /index.php?page_id=8&proID=$1&subID=$2 [L,QSA]
    
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
</IfModule>

【问题讨论】:

    标签: php wordpress .htaccess url-rewriting


    【解决方案1】:

    您可以再次尝试使用 htaccess 文件。您需要确保将此规则置于可能已经存在的 wordpress 规则之上:

    RewriteRule ^projects/([0-9]+)/([0-9]+)/?$ /index.php?page_id=8&proID=$1&subID=$2 [L,QSA]
    

    【讨论】:

    • 感谢您的回答。请看看我的“编辑2”。干杯。
    • 我正在审查我的旧问题。当时这并没有解决我的问题,但它有助于找到解决方案。我现在会接受。再次感谢。
    【解决方案2】:

    试试下面的代码。它可能会解决您的问题。我不确定。

    add_action('init', 'add_rewrite_rules');
    function add_rewrite_rules() {
      add_rewrite_rule('^projects/([^/]*)/?([^/]*)?/?','index.php?page_id=8&proID=$matches[1]&subID=$matches[2]','top');
      flush_rewrite_rules();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-01
      • 1970-01-01
      • 2013-11-23
      相关资源
      最近更新 更多