Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile .htaccess Redirection Error

    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?

  • #2
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #3
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    Try something like this:
    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]
    
    # Redirect /website directories
    RewriteRule ^websites/(.*)$ http://www.domain.com/$1/ [R=301,L]
    Not tested.

  • #4
    New Coder
    Join Date
    Oct 2011
    Location
    San Francisco, CA
    Posts
    21
    Thanks
    0
    Thanked 1 Time in 1 Post
    Try this

    Code:
    Redirect 301 /directory/ http://www.domain.com/websites/directory/
    tested for directory level redirection but not sure in your case it will work or not. it should be placed in the .htaccess of root.
    Search Engine Optimization, San Francisco Internet Marketing
    SEO Bay Area |AND| Custom Website Design


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •