RWS
RWS

Reputation: 560

.htaccess mod rewrite not doing redirects

I need to do a very basic mod rewrite on wordpress (with the default .htaccess settings). I need to redirect from category test1 to test2

What I attempt fails. I think im missing something basic here

<IfModule mod_rewrite.c>
Options FollowSymLinks
Options -MultiViews
RewriteEngine On
RewriteBase /

RewriteRule ^test1/$ test2/$ #returns 404

#alternatively this one returns 404 too: RewriteRule ^/test1/$ test2/$

</IfModule>

Upvotes: 1

Views: 56

Answers (1)

DocRoot
DocRoot

Reputation: 1201

RewriteRule ^test1/$ test2/$ #returns 404

If mod_rewrite is enabled and you tried this exact line then you've get a 500 Internal Server Error because you have an invalid flags argument. You need to explicitly state that this is a redirect with the R flag, otherwise you'll get an internal rewrite.

Also, the 2nd argument to the RewriteRule directive is an ordinary string, not a regex, so the trailing $ on test2/$ is invalid.

Any external redirects must also go above the existing WordPress directives (ie. the front-controller).

Try the following instead of the above:

Options +FollowSymLinks -MultiViews

RewriteEngine On

RewriteRule ^test1/$ /test2/ [R=302,L]

# BEGIN WordPress
# ... Rest of .htaccess file goes here

This redirects the single URL /test1/ to /test2/.

Change the 302 (temporary) to 301 (permanent) only after you have tested that it is working OK. (That is of course only if this should be permanent.)

The <IfModule> wrapper is not required here.


alternatively this one returns 404 too: RewriteRule ^/test1/$ test2/$

That wouldn't do anything in a per-directory (.htaccess) context, because of the slash prefix on the RewriteRule pattern.

The fact that you are getting a 404 for the first directive suggests these directives aren't actually being processed?!

Upvotes: 1

Related Questions