【问题标题】:Rewrite with htaccess only category into Wordpress仅使用 htaccess 将类别重写为 Wordpress
【发布时间】:2018-03-12 16:30:47
【问题描述】:

我只想将 URL 中的一个单词从拉丁语更改为西里尔语。这是一个 WordPress 产品,由旧开发人员在管理面板中设置为“产品”。他们使用自己的购物车模块开发了该网站,出于某种原因,如果我在管理面板中将“产品”更改为“продукт”,它会将我重定向到索引。我可以使用.htaccess 来直观地重写这个词,并且所有 URL 中的其他内容都保持不变吗?什么是正确的语法?

只是:

https://example.com/product/simple-product-1

收件人:

https://example.com/продукт/simple-product-1

当然还有所有其他产品。

【问题讨论】:

  • 直观地更改此 URL,您需要在 WordPress 中进行更改。

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


【解决方案1】:

这不是很简单,但有一种方法可以做到这一点,但有一些小的限制。

RewriteEngine On
RewriteBase /
# Check if the request is not coming from the Proxy
RewriteCond %{QUERY_STRING} !^(.*&)?is_proxy=true(&.*)?$
# Redirect (301) the user to the new location
RewriteRule ^/?product(/.*)?$ /продукт$1 [R=301,L]

# Rewrite the request with http_proxy including a Query String that we can check
RewriteRule  ^/?продукт(/.*)?$ /product$1?is_proxy=true [QSA,P]

# Common WP rewrite rule
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

我在带有 Apache 2.4.27 的 Ubuntu 16.04.03 LTS 服务器上对此进行了测试。

所以这里是先决条件:

  1. 您需要启用 apache http_proxy,否则您将收到 500 服务器错误。

  2. 我们必须添加一个查询字符串,以便重写引擎不会陷入无限循环。
    此查询字符串可以是您喜欢的任何内容,但请选择对您的 WP 站点没有意义的内容,例如来自 Google 的 UTM 参数。我的例子:is_proxy=true。用户不会看到查询字符串,但如果您选择了 WP 识别的内容,您可能会遇到一些意外行为。

如果您使用 mod_proxy,请同时阅读以下部分,了解如何保护您的服务器:

【讨论】:

    【解决方案2】:

    每当您register_post_type() 时,您都可以在参数中传入rewrite 参数来重写它。无需手动编写正则表达式或任何东西。

    https://codex.wordpress.org/Function_Reference/register_post_type

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-08
      • 1970-01-01
      • 2013-10-05
      • 2012-09-13
      • 2021-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多