简体   繁体   English

htaccess替换Wordpress中URL的一段?

[英]Htaccess replace a segment of the URL in Wordpress?

Problem问题

I would like to redirect from website.com/PRODUCT/name-of-item to website.com/COURSES/name-of-item .我想从website.com/PRODUCT/name-of-item重定向到website.com/COURSES/name-of-item There are too many products to do them individually.有太多产品无法单独完成。

Also, as the term product does appear in the name-of-item part sometimes (eg. product-best-rating ), I need to be mindful of that.此外,由于product一词有时确实出现在name-of-item部分(例如product-best-rating ),我需要注意这一点。

Many users have asked similar questions.许多用户提出了类似的问题。 I have tried the solutions and they do not work for me.我已经尝试了这些解决方案,但它们对我不起作用。

What I tried - Solution 1我尝试了什么 - 解决方案 1

Solution 1: Rewrite only the middle part of URL - mod rewrite .htaccess解决方案1: 仅重写URL的中间部分 - mod rewrite .htaccess

My code for Solution 1 is this:我的解决方案 1 的代码是这样的:

# 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 ^product/(.*)$ /courses/$1 [R=302,NE,L]
</IfModule>
# END WordPress

Results: https://website.com/product/normal-product/ still redirects to https://website.com/product/normal-product/结果: https://website.com/product/normal-product/仍然重定向到https://website.com/product/normal-product/

What I tried - Solution 2我尝试了什么 - 解决方案 2

Solution 2: https://webmasters.stackexchange.com/questions/102402/how-to-bulk-redirect-urls-and-replace-one-path-segment解决方案 2: https://webmasters.stackexchange.com/questions/102402/how-to-bulk-redirect-urls-and-replace-one-path-segment

For Solution 2, my code would be this:对于解决方案 2,我的代码是这样的:

# 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 ^/?product/(.*)$ https://website.com/courses/$1 [R=301,L]
</IfModule>
# END WordPress

Results: https://website.com/product/normal-product/ still redirects to https://website.com/product/normal-product/结果: https://website.com/product/normal-product/仍然重定向到https://website.com/product/normal-product/

I have checked it and don't see anything that is conflicting.我已经检查过了,没有看到任何冲突的地方。

The WordPress front-controller itself is conflicting with the redirect. WordPress 前控制器本身与重定向冲突。 By placing your directive after the WP directives, it's simply never going to get processed.通过将您的指令放在 WP 指令之后,它永远不会被处理。

You need to place your redirect (the directive from either solution 1 or 2 looks fine) before the WordPress front-controller.您需要在 WordPress 前控制器之前放置重定向(来自解决方案 1 或 2 的指令看起来不错)。 In fact, you should place it before the # BEGIN WordPress block (near the start of the file), since WordPress will try to maintain that part of the code and will overwrite any custom directives.实际上,您应该将它放在# BEGIN WordPress块之前(靠近文件开头),因为 WordPress 将尝试维护该部分代码并覆盖任何自定义指令。

For example:例如:

RewriteRule ^product/(.*) /courses/$1 [R=302,NE,L]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
:

You do not need to repeat the RewriteEngine On directive.您不需要重复RewriteEngine On指令。

The trailing $ on the RewriteRule pattern is superfluous since regex is greedy by default. RewriteRule模式的尾随$是多余的,因为正则表达式默认是贪婪的。


UPDATE: When you said "you don't need to repeat RewriteEngine On", do you mean that I can remove A Or do you mean B in this diagram...更新:当你说“你不需要重复 RewriteEngine On”时,你的意思是我可以删除 A 还是你的意思是这张图中的 B ......

htaccess

With WordPress, you should avoid manually editing the code in any section that is maintained by WordPress (or a plugin).使用 WordPress,您应该避免手动编辑由 WordPress(或插件)维护的任何部分中的代码。 If you do edit this code it is likely going to be overwritten later (by WordPress).如果您确实编辑了此代码,它可能会在以后被覆盖(由 WordPress)。

For this reason you should never manually edit the code between the # BEGIN WordPress and # END WordPress comment markers.因此,您永远不应手动编辑# BEGIN WordPress# END WordPress注释标记之间的代码。 I'm assuming the same applies to the # BEGIN Security Block as well.我假设这同样适用于# BEGIN Security Block So, you should probably leave both the RewriteEngine On directives in-place (although the first one is actually superfluous).因此,您可能应该将两个RewriteEngine On指令留在原地(尽管第一个实际上是多余的)。

The reason why you get multiple RewriteEngine On directives in a WordPress .htaccess files is because you potentially have multiple plugins all editing the .htaccess file and injecting their own directives.在 WordPress .htaccess文件中获得多个RewriteEngine On指令的原因是因为您可能有多个插件都在编辑.htaccess文件并注入自己的指令。 Each one will include a RewriteEngine On directive, since they are unaware of what else is in the .htaccess file.每个都将包含一个RewriteEngine On指令,因为他们不知道.htaccess文件中还有什么。

However, from an Apache/.htaccess perspective only the very last instance of the RewriteEngine directive does anything - and this controls the entire file.但是,从 Apache/.htaccess 的角度来看,只有RewriteEngine指令的最后一个实例可以做任何事情——它控制着整个文件。 So, if you are manually writing these directives then you would just have one RewriteEngine On directive at the top (just because that's more readable - but the order does not strictly matter).因此,如果您手动编写这些指令,那么您只会在顶部有一个RewriteEngine On指令(只是因为它更具可读性 - 但顺序并不重要)。

You could write a RewriteEngine Off directive at the very end of the file and this would disable the rewrite engine for the entire file, despite there being RewriteEngine On directives earlier in the file - these are essentially ignored.您可以在文件的最后编写一个RewriteEngine Off指令,这将禁用整个文件的重写引擎,尽管在文件的前面有RewriteEngine On指令 - 这些基本上被忽略了。 This is actually a quick way to "comment out" all the mod_rewrite directives, rather than manually using # in front of every directive.这实际上是一种“注释掉”所有 mod_rewrite 指令的快速方法,而不是在每个指令前手动使用#

So, if the # BEGIN WordPress block is still in place (with it's RewriteEngine On directive) - which you should not edit - then there is no need to repeat the RewriteEngine On directive if you create your own custom redirects above this.因此,如果# BEGIN WordPress块仍然存在(使用它的RewriteEngine On指令) - 您不应该对其进行编辑 - 如果您在此之上创建自己的自定义重定向,则无需重复RewriteEngine On指令。 That's all I was really saying initially.这就是我最初真正想说的。

Please try with the following:请尝试以下方法:

RewriteEngine on
RewriteRule ^/?product/(.*)$ /courses/$1 [R=302,NE,L]

I've tested here and this URL我在这里和这个 URL 测试过

https://website.com/product/name-of-item

redirects to重定向到

https://website.com/courses/name-of-item

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM