Forum Moderators: phranque

Message Too Old, No Replies

Redirect Entire Directory

         

Manny89

5:47 pm on Dec 6, 2011 (gmt 0)

10+ Year Member



I'm currently reconfiguring my web server and as part of it, I need to move some files around the server.

I am going to be hosting multiple sites on the server and have created a 'websites' directory inside the root.

An example URL would be as follows:
http://www.domain.com/websites/site1/


Using .htaccess, I want to rewrite the URL to leave out 'websites' and leave the URL as follows:
http://www.domain.com/site1/


If the user enters the first URL, I want to redirect them to the second. The aim is to redirect people to the second URL without any redirection errors appearing because of .htaccess files in each individual site directory.

I would also like to force a trailing slash at the end of the URL to make it act like a directory.

I have searched the web and posted on other boards but yet to find a working solution.

mememax

6:01 pm on Dec 6, 2011 (gmt 0)

10+ Year Member



In my little coding experience I would made this:

RewriteRule ^websites/(.*) [domain.com...] [R=301,L]

If you want to redirect www.domain.com/websites/ to www.domain.com/

But if you want to maintain the folder [domain.com...] as index for your websites listing you can change the * with a +

Manny89

6:24 pm on Dec 6, 2011 (gmt 0)

10+ Year Member



Inside the .htaccess file in the public_html root, I have the following line:


RewriteRule ^(.*)/(.*)$ /websites/$1/$2 [L]


With that, the following URLs will give the same content.

http://www.domain.com/websites/site1/index.php

AND

http://www.domain.com/site1/index.php


Inside the 'websites' directory, there is another .htaccess file with the following line.


RewriteRule ^(website1|website2)/$ http://www.domain.com/$1/ [R=301,L]


The aim is to get the URL to redirect, but all I get is a redirection error in all browsers.

g1smd

2:04 am on Dec 7, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Never use (.*) or (.+) at the beginning or in the middle of a pattern. I've posted about this almost every day for the last week; e.g. [webmasterworld.com...]

Use example.com in this forum to stop the auto-linking function.

lucy24

11:54 pm on Dec 7, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



RewriteRule ^(website1|website2)/$ http://www.example.com/$1/ [R=301,L]

The aim is to get the URL to redirect, but all I get is a redirection error in all browsers.

Good. That means the browsers are on their toes.

Request (whether from human or previous Rule):
http://www.example.com/website1/
will be redirected to
http://www.example.com/website1/

Like all redirects, this means the request now goes out the virtual door and starts over again at the top:

Fresh request for:
http://www.example.com/website1/
is once again redirected to
http://www.example.com/website1/

The browser, which in certain respects is more intelligent than the server*, can see where this is leading to. So it simply puts its foot down and says "Nope, this is going nowhere fast."


* By default, this would have to happen ten times before Apache figures out what's going on and slams the 403 or 500 lid on the whole thing.