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'
  • "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. – MrWhite Dec 30 '16 at 12:32
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.

  • Just tried without success. I post above here my new htaccess file. You can see your rule close to the end – bobrock4 Dec 29 '16 at 21:40
  • What is the error log? – marcanuy Dec 29 '16 at 22:35
  • Simply it shows the same old urls – bobrock4 Dec 29 '16 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. – MrWhite Dec 30 '16 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.