HLLL
HLLL

Reputation: 3751

.htaccess 301 redirects wordpress

I'm trying top redirect a standard Wordpress "folder" URL of a page to a new one on the same domain while keeping all subfolders, query strings etc. From domain/old-path/ to domain/new-path/.

Now I have this:

RedirectMatch 301 ^/old-path(.*) /new-path$1

It works, but domain/old-path redirects to domain/new-path and, this being WP, another redirect happens from domain/new-path to domain/new-path/.

I would like both domain/old-path and domain/old-path/ to redirect straight to domain/new-path/ with the ending /. All this while, like I said, keeping all "subfolders", query strings etc.

Upvotes: 2

Views: 302

Answers (1)

MrWhite
MrWhite

Reputation: 45829

...this being WP, another redirect happens from example.com/new-path to example.com/new-path/.

If /new-path is a physical directory then it's not WP that is triggering the redirect, it is Apache's mod_dir that is "fixing" the URL.

However, since you are using WordPress, it is preferable to use mod_rewrite (RewriteRule) to perform the redirect instead of a mod_alias RedirectMatch in order to avoid conflicts. Different modules execute at different times during the request and WP already uses mod_rewrite.

Try something like the following at the top of your .htaccess file before any existing WP directives:

RewriteRule ^dir/?(.*) /new-dir/$1 [R,L]

As you can see, although the slash is optional in the RewriteRule pattern, this is never captured by the regex and the slash is always present in the substitution.

Change the R (temporary) redirect to a R=301 only when you are sure it's working OK.

Upvotes: 1

Related Questions