Redirect htaccess does not work correctly with my new wordpress site

Redirect permanent / http://www.example.com/wordpress/

You can’t use a mod_alias Redirect directive in this way to redirect to a subdirectory on the same site because the Redirect directive is prefix-matching and everything after the match is appended to the target (which is how this directive is able to redirect all URL-paths to the same URL-path at the target). So the above naturally redirects like so:

  • http://example.com/ – Initial request
  • http://example.com/wordpress/ – First redirect
  • http://example.com/wordpress/wordpress/ – Second redirect, because the directive matches / and everything after the match (ie. wordpress/) is appended to the end of the target (ie. http://www.mysite.fr/wordpress/) to become http://www.mysite.fr/wordpress/ + wordpress/.
  • etc.

Which is a “redirect loop”.

Try the following instead…

Assuming the WordPress .htaccess is located at /wordpress/.htaccess (ie. in the /wordpress) subdirectory, then you could perform this redirect using mod_rewrite in the root .htaccess file (at /.htaccess) instead. For example:

RewriteEngine On
RewriteRule !^wordpress/ /wordpress%{REQUEST_URI} [R=302,L]

This directive states that if the URL-path does not already start /wordpress/ (the slash prefix is omitted in .htaccess) then prefix /wordpress to the requested URL-path. (The REQUEST_URI server variable already contains a slash prefix.)

This assumes you don’t need to access your old website in the document root. If you do then you will need additional conditions (RewriteCond directives).

Only change the above 302 (temporary) redirect to 301 (permanent) – if this is intended to be a permanent change – after you have tested to make sure it is working OK.

You will need to clear your browser cache before testing, as any erroneous 301s are likely to have been cached by the browser.


UPDATE: (From comments…)

I have tried your suggestion as to the .htaccess file at the root of the old site with this content :

RewriteEngine On
RewriteRule !^wordpress/ /wordpress%{REQUEST_URI} [R=302,L]
Redirect permanent / http://www.mysite.fr/wordpress/

and I get the answer : ERR_TOO_MANY_REDIRECTS The redirect loop is still not solved. Do you have any idea how I could solve this?

You need to remove the Redirect directive. So, it should simply read:

RewriteEngine On
RewriteRule !^wordpress/ /wordpress%{REQUEST_URI} [R=302,L]

And make sure to clear your browser cache before testing.