1

I need to 301 redirect: example.com/service-1.html to example.com/our-services/1/.

I've tried redirect plugins and .htaccess:

  1. Redirect /service-1.html /our-services/1/

  2. Redirect 301 /service-1.html http://www.example.com/our-services/1/

  3. RewriteRule ^service\-1\.html$ /our-services/1/? [L,R=301]

  4. RewriteRule ^(.*)/service-1.html$ http://www.example.com/our-services/1/$1 [R=301,L]

But nothing is working, every time I'm redirected to (Woocommerce) product page product/1.

Wordpress .htaccess file:

# 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]
</IfModule>
# END WordPress

What am I doing wrong?

0

You may be adding the directive to the wrong part of your .htaccess file? It needs to go at the top, before the existing WordPress directives. You should also use RewriteRule (ie. mod_rewrite) since you have existing mod_rewrite directives (the WP front controller).

Something like your 3rd attempt at the top of your .htaccess file should result in the appropriate redirect. For example:

RewriteRule ^service-1\.html$ /our-services/1/? [R=302,L]

Change to a 301 (if that is the intention) only when you are sure it's working OK. 301s are cached hard by the browser, so you will need to ensure the browser cache is cleared before testing.

The ? on the end of the RewriteRule substitution simply removes the query string from the request. Whether this is required or not is up to you, but you don't have a query string in your example.

  • Thanks you, it is working. May I use only sugested code line or should be code embed <IfModule mod_rewrite.c> RewriteEngine On </IfModule>? – Gore May 19 '17 at 7:42
  • Yes, you can use just that one line of code at the top of the file before the # BEGIN WordPress block, providing there is a RewriteEngine On directive somewhere in the file. You do not need to wrap it in an <IfModule> container. I would avoid changing what's inside the # BEGIN WordPress ... # END WordPress block simply because WordPress might overwrite it in a future update. – MrWhite May 19 '17 at 8:29
  • Thanks you. Because I need redirect a lot of links and there is possibility of changing category base name "our-services" in future, I have one additional question: Is it possible to have category base relative in .htaccess? "our-services" = woocommerce product category base. – Gore May 19 '17 at 10:37
  • 1
    You could potentially assign "our-services" to an environment variable and use that in your directives instead - if the idea is to prevent "our-services" being hardcoded in every directive? Or you could potentially change the RewriteBase directive and use relative substitutions? But note that RewriteBase will affect the entire file. (RewriteBase is not currently being used in the code you posted.) – MrWhite May 19 '17 at 11:39

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.