-2

I want to redirect some URL's using htaccess file.

My htaccess files is as under:

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

Redirect /old-post-type/old-parent-post/old-post-slug/ /new-post-type/new-post-slug/

***** Some More URLs *****

# END WordPress

It should redirect to http://yourwebsitedomain.com/new-post-type/new-post-slug/ but, it is redirecting to http://yourwebsitedomain.com/new-post-type/new-post-slug/old-post-slug/

"old-post-slug/" automatically added to last I don't know why?

Any suggestions accepted.

Jaydip Nimavat
  • 603
  • 9
  • 19

2 Answers2

0

I wouldn't recommend mixing mod_alias and mod_rewrite like that, the RewriteRule runs first, then runs again after Redirect, making strange things happen. Just use mod_rewrite all the way through, with the redirect before the other rules and with L flag, like so:

RewriteEngine On
RewriteBase /
#I put 302, use 301 if permanent redirect is what you are after.
RewriteRule ^old-post-type/old-parent-post/old-post-slug/$ /new-post-type/new-post-slug/ [L,R=302] 
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

And this only if you really must use htaccess for the redirects, like someone mentioned wordpress already has functionality for dealing with redirects.

Vrac
  • 1,054
  • 1
  • 8
  • 15
  • "old-post-type/old-parent-post/old-post-slug" this is example, it will be post type name and I have many of URLs with different post types so for every URL have RewriteRule ? – Jaydip Nimavat Sep 14 '16 at 09:47
  • No you don't need a rule for every url, you can use capture groups to write a generic rule to redirect. You haven't really provided enough information about what you are redirecting from, and to, in order for someone to provide a working rule for your site. I suggest reading: http://stackoverflow.com/questions/20563772/reference-mod-rewrite-url-rewriting-and-pretty-links-explained for example, to familiarize yourself with the concepts. There are lots of links in the answers to other questions/answers of people solving similar problems. – Vrac Sep 14 '16 at 10:06
  • I have trying below URLs first one from my old site and trying to redirect with new site: `Redirect /featured-hotels/half-moon-bay-lodge-northern-california-coast/lodge-golf-links/ /featured-hotels/half-moon-bay-lodge/ ` But it will redirect to /featured-hotels/half-moon-bay-lodge/lodge-golf-links/ /lodge-golf-links/ automatically added to last, I have too many old URLs like this, and I want to redirect all for SEO purpose. – Jaydip Nimavat Sep 14 '16 at 10:13
-2

Try this:

Redirect 301 /old-post-type/old-parent-post/old-post-slug/ http://your-domain.com/new-post-type/new-post-slug/

This should work. The 301 stands for "Moved Permanently".

Adrian Lambertz
  • 913
  • 6
  • 11