So, I've at site where I would like pages for example

www.example.com/shop/index.php

(or other page under /shop/ for your matter.) to now appear as

www.example.com/index.php

but without really moving the files, that are still situated under /shop/ but they are now utilized via web addresses at /

Appears not so difficult, and that i can perform that by itself utilizing a RewriteRule, pretty standard. The 2nd requirement would be to keep your links towards the old content working, and so i desire a similar factor however in another direction, but utilizing a 301 exterior redirect (not rewrite), i.e.

www.example.com/shop/index.php

should 301 redirect to

www.example.com/index.php

Again, not so difficult by itself. But place the two together and also you it offers a superior an attractive "This web site includes a redirect loop" error when being able to access a webpage. However, I do not realise why because one rule is really a rewrite and something is really a 301 redirect plus they have the L flag and so i thought forget about rules were processed. So, I'm in the limit of my knowledge of this mod_rewrite stuff.

To check, and steer clear of mucking up my website for individuals going to, I'm while using sites /blob/ and /b/ with /b/ being the new directory. The code I presently obtain that provides the redirect loop is

RewriteRule ^blob/(.*)$ "http\:\/\/www\.example.com\/b\/$1" [R=301,NC,L]
RewriteRule ^b/(.*)$ /blob/$1 [NC,L]

I suppose its since the first rule has been performed again when the second has, why when the 'L' flag can be used? And what condition must i check or switch to make to ensure that the redirect rule only will get triggered when the original request is perfect for /blob/ not the rewritten URI.

Try the next (I made use of your actual url good examples):

RewriteEngine On

RewriteCond %{QUERY_STRING} !rewrite
RewriteRule ^shop/(.*)$ http://localhost/$1 [R=301,NC,L]

RewriteCond %{REQUEST_URI} !^\/shop\/
RewriteRule ^(.*)$ /shop/$1?rewrite [NC,L,QSA]

The problem you're encountering is the fact that when mod_rewrite does the rewrite it's really carrying out an Internal Redirect, which in turn causes the guidelines to applied again. So, which means you need to safeguard against that, within this situation, I have added a parameter rewrite towards the URL the redirect inspections for.

Hope that can help.

With no necessity of an additional query parameter you should use following rules inside your .htaccess file:

Options +FollowSymlinks -MultiViews
RewriteEngine on

RewriteCond %{THE_REQUEST} ^GET\s/shop/ [NC]
RewriteRule ^shop/(.*)$ /$1 [R=301,NC,L]

RewriteCond %{REQUEST_URI} !^/shop/ [NC]
RewriteRule ^(.*)$ /shop/$1 [L]
  • First rule will be sending 301 to the request from /shop/foo to /foo within the browser, that is an exterior redirect
  • Second rule will internally redirect any /foo to /shop/goo without altering URL within the browser thus ensuring your actual files are offered from $DOCUMENT_ROOT/shop/ directory.