0

I have recently changed how posts are categorized and changed the permalinks not to show the category. Consequently, I need to redirect the old URLs. I have tried many variations of the following but nothing seems to work:

RewriteEngine On
RewriteRule ^postname http://www.example.com/postname [R=301,L]


# 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

I have also tried the following:

  • instead of RewriteRule, used Redirect 301 ...
  • used the full URL for the original (instead of the Regex '^')
  • I have moved the redirection statement in different places i.e. inside the # BEGIN WordPress, as well as place it after the IfModule, RewriteEnging On, etc.
  • I have also tried both RewriteRule and Redirect at the end of the file after # END WordPress

I have been using Chrome Incognito to test after each change to avoid the caching issues. I actually have about 40 posts that I would have to redirect to not use their old categories/sub-categories in the URL. I read somewhere that the wordpress code will redirect/rewrite to index.php so that the internal Wordpress permalinks would work but I am unsure how this interacts with the redirects that I wish to include.

I am not sure what I am missing. Any help would be greatly appreciated!

0

The ^ in your rewriterule means the URL must begin with the postname, as in your new structure - so what you're saying in that second line is, "redirect http://www.example.com/postname to http://www.example.com/postname".

So it's the ^ that's throwing things off.

If you were previously using URLs like http://www.example.com/categoryname/postname/ then I would suggest instead:

RewriteEngine On
RewriteRule ^categoryname/(.*)$ http://www.example.com/$1 [R=301,L]
RewriteRule ^categoryname2/(.*)$ http://www.example.com/$1 [R=301,L]
RewriteRule ^categoryname3/(.*)$ http://www.example.com/$1 [R=301,L]

# 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

Just add a RewriteRule on its own line for every category. The "$" tells the server to save everything that comes after the category name (i.e. your post slug) and redirect to example.com/that-particular-slug.

  • Hi WebElaine, That worked! I have been trying to figure different options for weeks and trying to learn regex just to figure it out (obviously I still have much to learn!). I have one question - In my RewriteRule, I have also tried using the full URL for the pattern and provided a full URL for the substitution but that didn't seem to work. For example: code RewriteRule www.example.com/category/postname example.com/postname [R=301,L] – E.Hoh Jun 15 '17 at 15:46
  • Glad it worked. Your code didn't paste - you might need to edit your original question in order for it to appear, or wrap it in backticks ` `. – WebElaine Jun 15 '17 at 15:48
  • Thank's for that. The example was For example: RewriteRule www.example.com/category/postname http://www.example.com/postname [R=301,L] - this never worked for some reason either. In any case, I am very grateful for your help !! – E.Hoh Jun 15 '17 at 15:52
  • Hmm, redirects are fairly finicky, so I'd say it may have had something to do with your rule starting out with www.example.com. There's a book called "Htaccess Made Easy" which explains almost anything you would ever want to do in a clear format. I would recommend it if you ever need more than just a couple of lines like this. winningwp.com/htaccess-made-easy-book-review – WebElaine Jun 15 '17 at 15:59
  • Thank you for the book recommendation too. I will check it out. – E.Hoh Jun 15 '17 at 16:46

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.