Jump to content

.htaccess Rewrite And Redirect


Manny

Recommended Posts

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.

# Ensure the URL ends with a trailing slashRewriteRule ^directory$ http://www.domain.com/directory/ [R=301,L] # Rewrite the URL to grab the contents from the websites directoryRewriteRule ^directory/(.*)$ /websites/directory/$1 [L]

Then, inside the 'websites' directory, I have the following lines in another .htaccess file

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 works absolutely fine and redirects to http://www.domain.com/directory/ However, http://www.domain.com/websites/directory/ does not redirect. I have also tried changing the .htaccess code in the 'websites' directory, as follows:

# Make trailing slash optionalRewriteRule ^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?

Link to comment
Share on other sites

Apologies for double posting. Having read back through my original post, I realise it's a little long winded so I'll try to cut it down. Basically, the current URL of my site would be:

http://www.domain.com/websites/directory

Using .htaccess, I can rewrite the URL to leave out 'websites' and leave the URL as follows:

http://www.domain.com/directory

However, 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 the redirection error that appears. I would also like the trailing slash at the end of the URL to be forced.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...