【问题标题】:.htaccess rewrite without redirect / Wordpress.htaccess 重写没有重定向/Wordpress
【发布时间】:2022-04-28 23:16:43
【问题描述】:

我想重写一个 URL,以便在浏览器中显示“few_words_Keyword”,但页面显示 /directory/Keyword

在我的 htaccess 中,我有以下内容:

RewriteEngine On
Options +FollowSymLinks -MultiViews
RewriteBase /
RewriteRule few_words_(.*)$ /directory/$1 [L,R,NC]

它适用于 R 并重定向到页面 /directory/Keyword 但没有 R 我得到 404 错误。

编辑:URL /directory/$1 不是“真实的”,它是另一个由 Wordpress 管理的重定向:

    <IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

你能帮帮我吗?

在此先感谢

【问题讨论】:

  • 那么/directory/Keyword是什么?那是物理上存在的文件或目录吗?或者您是否有更多的重写,以获取此 URL 以提供所需的内容?
  • 感谢您的提问。很好的一点。这是另一个重定向,它有效,但位于 htaccess 的下方。它是通过 Wordpress
  • WP 路由通过检查 $_SERVER['REQUEST_URI'] 包含的内容来工作 - 当您仅进行内部重写时,该值不会改变。我认为与其通过 .htaccess 处理这个问题,不如在这里使用codex.wordpress.org/Rewrite_API/add_rewrite_rule
  • 非常感谢这是解决方案!非常感谢
  • 请将其添加为答案,以从未回答的问题队列中删除此问题并帮助其他读者。谢谢。

标签: wordpress apache .htaccess


【解决方案1】:

用这里的文档解决了https://codex.wordpress.org/Rewrite_API/add_rewrite_rule

我把它放在我的子主题的function.php中

/* Redirection */ 
  function custom_rewrite_rule() {
    add_rewrite_rule('^xxxxxxxxxxxxx(.*)','/index.php?p=123&var=$matches[1]','top');
  }
  add_action('init', 'custom_rewrite_rule', 10, 0);

【讨论】:

    猜你喜欢
    • 2013-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多