xojijog684
xojijog684

Reputation: 310

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. 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.

Many users have asked similar questions. I have tried the solutions and they do not work for me.

What I tried - Solution 1

Solution 1: Rewrite only the middle part of URL - mod rewrite .htaccess

My code for Solution 1 is this:

# 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/

What I tried - Solution 2

Solution 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:

# 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/

Upvotes: 2

Views: 1280

Answers (2)

MrWhite
MrWhite

Reputation: 45829

I have checked it and don't see anything that is conflicting.

The WordPress front-controller itself is conflicting with the redirect. By placing your directive after the WP directives, it's simply never going to get processed.

You need to place your redirect (the directive from either solution 1 or 2 looks fine) before the WordPress front-controller. 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.

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.

The trailing $ on the RewriteRule pattern is superfluous since regex is greedy by default.


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...

htaccess

With WordPress, you should avoid manually editing the code in any section that is maintained by WordPress (or a plugin). If you do edit this code it is likely going to be overwritten later (by WordPress).

For this reason you should never manually edit the code between the # BEGIN WordPress and # END WordPress comment markers. I'm assuming the same applies to the # BEGIN Security Block as well. So, you should probably leave both the RewriteEngine On directives in-place (although the first one is actually superfluous).

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. Each one will include a RewriteEngine On directive, since they are unaware of what else is in the .htaccess file.

However, from an Apache/.htaccess perspective only the very last instance of the RewriteEngine directive does anything - and this controls the entire file. 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).

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. This is actually a quick way to "comment out" all the mod_rewrite directives, rather than manually using # in front of every directive.

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. That's all I was really saying initially.

Upvotes: 2

Alberto
Alberto

Reputation: 12939

Please try with the following:

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

I've tested here and this URL

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

redirects to

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

Upvotes: 0

Related Questions