I'm currently reconfiguring my web server and as part of it, I need to move some files around the server.
Currently, I am using the following URL:
http://www.domain.com/directory
The directory being called in the URL is a subdirectory of the root public_html directory. Now, I am going to be hosting multiple sites on the server and have created a 'websites' directory inside the root. The directory that was previously called in the URL has been moved into the 'websites' directory.
As the directory has been moved, the URL would now have to read:
http://www.domain.com/websites/directory
What I want to do is use .htaccess to ensure the old URL remains live and anybody using the second URL is automatically redirected.
Inside my root directory, I have the following code inside a .htaccess file.
Code:
# Ensure the URL ends with a trailing slash
RewriteRule ^directory$ http://www.domain.com/directory/ [R=301,L]
# Rewrite the URL to grab the contents from the websites directory
RewriteRule ^directory/(.*)$ /websites/directory/$1 [L]
Then, inside the 'websites' directory, I have the following lines in another .htaccess file
Code:
RewriteRule ^directory$ http://www.domain.com/directory/ [R=301,L]
Now this is all working fine, with one exception.
http://www.domain.com/websites/directory (no trailing slash) works absolutely fine and redirects to http://www.domain.com/directory/
However, http://www.domain.com/webites/directory/ (with trailing slash) does not redirect.
I have also tried changing the .htaccess code in the 'websites' directory, as follows:
Code:
# Make trailing slash optional
RewriteRule ^directory/?$ http://www.domain.com/directory/ [R=301,L]
This redirects as intended, but then a redirection error appears across all browsers. If this error can be resolved, then the intended solution would be working perfectly. What am I doing wrong?