6

I have several URLs in a WordPress website I would like to rewrite and then make a 301 redirect.

URLs are of the form

www.example.com/prodotto/something-after

These URLs should be changed to

www.example.com/shop/something-after

Should I use URL rewrite rules in .htaccess? I have used this rule, but it doesn't work:

# 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 ^/?prodotto/(.*)$ http://www.example.com/shop/$1 [R=301,L]
</IfModule>
# END WordPress'
1
  • "I would like to rewrite and then make a 301 redirect" - Rewriting first doesn't make sense (at least not in this instance). You simply want to 301 redirect. Commented Dec 30, 2016 at 12:32

1 Answer 1

1

You can use Apache Module mod_rewrite:

RewriteEngine ON
RewriteRule ^/?stringtochange/(.*)$ http://www.domainname.com/newstring/$1 [R=301,L]

The string after stringtochange is passed to the other url containing newstring in the $1 variable.

So for example, if you access www.example.com/stringtochange/cool-page.html it will get redirected to www.example.com/newstring/cool-page.html.

4
  • Just tried without success. I post above here my new htaccess file. You can see your rule close to the end Commented Dec 29, 2016 at 21:40
  • What is the error log? Commented Dec 29, 2016 at 22:35
  • Simply it shows the same old urls Commented Dec 29, 2016 at 22:38
  • 1
    @bobrock4 The order of these directives is important. The above RewriteRule (redirect) needs to go before your existing WordPress directives. By placing the rule after the WP rewrites it will never be executed (because the WP front controller rewrites everything). As a general rule, external redirects should always come before internal rewrites. Commented Dec 30, 2016 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.