1

I have an old website that works fine (www.example.com). I have created a new website (www.example.com/wordpress) and I thought I could use .htaccess to redirect as I've done it for other html websites in the past.

I get the result: www.example.com/worpress/wordpress/wordpress......... until it's in error.

Can someone help me?

I used this command in .htaccess:

Redirect permanent / http://www.example.com/wordpress/
0
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.

| improve this answer | |
  • Thanks for taking time to answer my question. The .htaccess file was a the root of the old site, what needs to be changed then ? – Philippe R Aug 27 '18 at 17:09
  • As mentioned above, add the two directives stated: RewriteEngine and RewriteRule to the root .htaccess file (of the old site). If you have any existing directives in that file then they should probably be removed. – MrWhite Aug 27 '18 at 19:59
  • I have tried your suggestion as to the .htaccess file at the root of the old site with this content : – Philippe R Aug 28 '18 at 8:23
  • 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 / 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 ? Thanks – Philippe R Aug 28 '18 at 8:30
  • 1
    I did what you suggested in your correction and for some reason I had to exit the ftp and reconnect to reload the page. It now works !!! Many Many thanks MrWhite :D !!! – Philippe R Aug 28 '18 at 11:06

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.