-2

I am trying to redirect any request for a directory by appending /public onto the end. Adding this one line into my .htaccess file:

Redirect 301 /somedirectory/ http://www.my-site.com/somedirectory/public/home.shtml

and then attempting to access www.my-site.com/somedirectory results in the browser (Firefox on desktop, but have also tried Edge on desktop and Firefox on mobile with the same result) displaying this:

The page isn't redirecting properly Firefox has detected that the server is redirecting the request for this address in a way that will never complete. This problem can sometimes be caused by disabling or refusing to accept cookies.

and the URL displayed in the address bar is this:

http://www.my-site.com/somedirectory/public/home.shtmlpublic/home.shtmlpublic/home.shtmlpublic/home.shtmlpublic/home.shtmlpublic/home.shtmlpublic/home.shtmlpublic/home.shtmlpublic/home.shtmlpublic/home.shtmlpublic/home.shtmlpublic/home.shtmlpublic/home.shtmlpublic/home.shtmlpublic/home.shtmlpublic/home.shtmlpublic/home.shtmlpublic/home.shtmlpublic/home.shtmlpublic/home.shtml

Does anyone know what's going on and how to correct please?

Guvv1981
  • 1
  • 1
  • Hi, interesting, not sure if this would be of interest https://stackoverflow.com/questions/21417263/htaccess-add-remove-trailing-slash-from-url – IronMan May 13 '20 at 20:22
  • Redirect matches on path _prefixes_, and appends the rest of the requested URL to the new one - `/somedirectory/` matches `/somedirectory/public/home.shtml` as a prefix, so this gets redirected again, and the excess part appended. You can not do this using the Redirect directive, this needs either RedirectMatch (with a pattern anchored at the end), or a RewriteRule. – CBroe May 14 '20 at 06:33
  • @CBroe - Got it, that makes sense. As I'm still learning this, I might try RedirectMatch before tackling the joys of rewrites! I guess I'll need something along the lines of `RedirectMatch 301 ^/somedirectory/(.*)$ http://www.my-site.com/somedirectory/public/$1` – Guvv1981 May 14 '20 at 07:33
  • @IronMan - Thanks for the suggestion, I had wondered whether trailing / leading slashes might be something to do with it – Guvv1981 May 14 '20 at 07:38

1 Answers1

0

After further experimentation, I have now solved this with a mod_rewrite. The behaviour I wanted was to forward to the /public directory except when requesting the root my-site.com, as my main landing page is in the root. So I've successfully used this:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^(www\.)?my-site\.com$ [NC]
    RewriteCond %{REQUEST_URI} /(directory\-one|directory\-two|directory\-three)/?$
    RewriteRule ^(.*)$ /%1/public/ [R,L,END]
</IfModule>

As I currently only have 3 directories on which I want this behaviour, all is well, but if and when I add more directories I might change the logic to exclude a few specific directories, rather than include a long list of many.

Guvv1981
  • 1
  • 1