SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Feb 2014
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    redirect non www to www and index.html to root (Subfolder)

    I want to redirect non www to www and index.html to root but it is not working, But when I type http://mydomain.net/about-us.html it redirects to http://www.mydomain.net/html/about-us.html


    Actually my site html pages are stored in subfolder named HTML
    eg

    /(Root)
    HTML
    about-us.html
    index.html


    I am using this code, but getting error when I type http://mydomain.net/about-us.html it redirects to http://www.mydomain.net/html/about-us.html

    RewriteEngine On

    RewriteCond %{HTTP_HOST} !^(www\.mydomain\.net)?$
    RewriteRule (.*) http://www.mydomain.net/$1 [R=301,L]

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html?\ HTTP/
    RewriteRule ^(([^/]+/)*)index\.html?$ http://www.mydomain.net/$1 [R=301,L]

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

    From your description, your HTML and html files are OUTSIDE the webspace defined by (Root). Therefore, you should not be able to access them using the virtual host as shown. Simply move your HTML directory and .html files into the (Root) directory and you'll be set.

    As for your mod_rewrite code, please allow a few constructive comments:

    Code:
    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} !^(www\.mydomain\.net)?$
    # because you're not using the %1 variable created by the (..), delete the parentheticals (MINOR machine cycle saver)
    RewriteRule (.*) http://www.mydomain.net/$1 [R=301,L]
    # I prefer to use the Apache variable {%REQUEST_URI} rather than capture it in $1, therefore, I use
    # RewriteRule .? http://www.mydomain.net%{REQUEST_URI} [R=301,L]
    
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html?\ HTTP/
    # OMG! No need to capture all that @#$% (again, you're not using it) so I would use
    # RewriteCond %{THE_REQUEST} index\.html
    RewriteRule ^(([^/]+/)*)index\.html?$ http://www.mydomain.net/$1 [R=301,L]
    # only one / but it's all optional? Loopy (on www.mydomain.net/index.html)!!! Make the * a + and you'll be okay
    # ... UNLESS your host requires Apache to display the DirectoryIndex.
    # Personally, I don't like stripping the DirectoryIndex filename but that's just me.
    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


Tags for this Thread

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
  •