SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot dizyn's Avatar
    Join Date
    Apr 2006
    Posts
    181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    htaccess 301 redirect

    Hi,

    I want to setup 301 redirect on following urls to one url given below:

    http://www.example.com
    http://www.example.com/index.htm
    http://example.com
    http://example.com/index.htm

    To

    http://example.com

    Is this possible using htaccess?

    thanks

  2. #2
    SitePoint Member
    Join Date
    Jan 2013
    Location
    1933 10th st, South Lake Tahoe, CA
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    The old location of the file has to be the absolute path from the root of your server. The new location should use http. So for example, if you want to move a file called http://www.example.com from the root of your site to a subdirectory called products you would use :

    The exact URL entered above: This will redirect the search spiders and users to the exact URL you entered in the “Redirect To:” example. For instance, if you somebody went to http://www.example.com/index.htm, they would go to http://example.com.

    A directory below this one: Freaky Friday images aside, this redirects a parent directory to a child directory. For instance, if someone typed in http://www.example.com/index.htm to access the site and you wanted the request to be redirected to a child directory named “example.com”, then they would be sent to http://example.com.

    OR


    The old location of the file has to be the absolute path from the root of your server. The new location should use http. So for example, if you want to move a file called productreview.html from the root of your site to a subdirectory called example you would use :

    Redirect 301 /http://www.example.com/index.htm http://example.com

  3. #3
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,645
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Hi CW!

    Welcome to SitePoint!

    Well, that was a good though long-winded explanation then you missed the correct format of the Redirect statement:

    Redirect {optional code} {internal absolute directory/file} {internal or external absolute redirection}

    The point is that the Redirect statement you used should NOT have the protocol along with the internal absolute directory/file.

    Code:
    Redirect 301 /http://www.example.com/index.htm [url]http://example.com
    Diz,

    Why would you ever want to redirect http://example.com to http://example.com? That's an infinite loop!

    Specificity:

    Redefining your stated objective:
    1. Remove www. from domain
    2. Remove index.htm from the URI (IMHO, a bad thing to do especially as your host may have Apache configured to display the DirectoryIndex).


    The short answer is "yes."

    Try:

    Code:
    RewriteEngine on
    
    # Remove www
    RewriteCond %{HTTP_HOST} ^www\. [NC]
    RewriteRule .? http://example.com%{REQUEST_URI} [R=301,L]
    
    # Remove index.htm from DocumentRoot
    RewriteRule ^index\.htm$ http://example.com [R=301,L]
    Of course, all this is explained in the mod_rewrite tutorial linked as an Article in my signature.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


Bookmarks

Posting Permissions

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