代码之家  ›  专栏  ›  技术社区  ›  Jack Robson

在WordPress中设置自定义重写规则

  •  0
  • Jack Robson  · 技术社区  · 6 年前

    我的目标是将URL从/t/code.png转换为/t/?t=code,以便在我的电子邮件跟踪中使用。

    根据其他答案中的示例,我将以下重写添加到我的functions.php中。

    function custom_rewrite_basic() {
      add_rewrite_rule('^/?t/(.*).png$', 't/?t=$matches[1]', 'top');
    }
    add_action('init', 'custom_rewrite_basic');
    

    我还尝试将它直接添加到.htaccess

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    RewriteRule ^/?t/(.*).png$ t/?t=$1 [L]
    </IfModule>
    
    
    # END WordPress
    

    重新启动了Apache2,但仍然重定向到404页。

    1 回复  |  直到 6 年前
        1
  •  0
  •   user9110002 user9110002    6 年前

    请尝试删除“?”前面的“/”字符。 /t/?t=代码 变成 /t?t=代码