Issue in redirecting non www to www and index.html to root

by setjo
20 replies
I want to redirect non www to www and index.html to root but it is not working, I have used the following code but 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]
#indexhtml #issue #redirect non www to www #redirecting #root #www
  • Profile picture of the author WPcrew
    You have .html?$ in the code, that's why it's adding html to url.
    Try this:

    Code:
    RewriteEngine on
    RewriteCond %{HTTP_HOST} !^www.yoursite.com
    RewriteRule (.*) http://www.yoursite.com/$1 [R=301,L]
    {{ DiscussionBoard.errors[8976054].message }}
  • Profile picture of the author WPcrew
    There are few solutions.
    First, can't you move your /html folder to root?
    Second, if you have cpanel, you can add domain to particular folder like /public_html/html.
    Tie it up for mysite.com. It should work.

    Third, if you can't solve it - look at this link: Redirecting Subdirectories to the Root Directory via HTAccess | Perishable Press. Good luck.
    {{ DiscussionBoard.errors[8980354].message }}
    • Profile picture of the author setjo
      I have tried to move pages to root but then the site gave error.
      I do not know how to do thru cpanel

      Pl share the complete code for htaccess to move non www to www and to rediect index.html to root
      {{ DiscussionBoard.errors[8980368].message }}
      • Profile picture of the author WPcrew
        Originally Posted by setjo View Post

        I have tried to move pages to root but then the site gave error.
        I do not know how to do thru cpanel

        Pl share the complete code for htaccess to move non www to www and to rediect index.html to root
        What kind of error? What's kind of site, wordpress, some cms, or plain html?
        Here's how to add a domain through cpanel, you just need set correct path (like /home/public_html/html, which you can see when you connect via ftp, to access your site files).
        {{ DiscussionBoard.errors[8980455].message }}
        • Profile picture of the author setjo
          It is a plain html site,pl help
          {{ DiscussionBoard.errors[8980471].message }}
  • Profile picture of the author phpg
    Add

    RewriteBase /

    after RewriteEngine On

    Change RewriteRule (.*) http://www.mydomain.net/$1 [R=301,L] to

    RewriteRule ^(.*)$ http://www.mydomain.net/$1 [R=301,L]
    {{ DiscussionBoard.errors[8980373].message }}
    • Profile picture of the author setjo
      I have added this code

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

      but when I type http://abc.net it gets redirected to http://www.abc.net/html/ and I got this message

      The requested URL /html/ was not found on this server.


      PL help
      {{ DiscussionBoard.errors[8980394].message }}
  • Profile picture of the author phpg
    Try RewriteBase /html/ instead of RewriteBase /
    {{ DiscussionBoard.errors[8980422].message }}
    • Profile picture of the author setjo
      not working
      because now when I type http://abc.net it redirects to
      http://www.abc.net/html/ and gives error


      RewriteEngine On
      RewriteBase /html/
      RewriteCond %{HTTP_HOST} !^(www\.abc\.net)?$
      RewriteRule ^(.*)$ http://www.abc.net/$1 [R=301,L]
      {{ DiscussionBoard.errors[8980443].message }}
  • Profile picture of the author phpg
    Do you have another .htaccess at the www root directory (on the same level as you /html directory)
    {{ DiscussionBoard.errors[8980473].message }}
    • Profile picture of the author setjo
      I have added code in the .htaccess file at root level but if you want then I will copy .htaccess file to HTML folder
      {{ DiscussionBoard.errors[8980482].message }}
  • Profile picture of the author phpg
    If your site is in /html folder, then why do you edit .htaccess in the root folder?
    {{ DiscussionBoard.errors[8980493].message }}
    • Profile picture of the author setjo
      I moved htaccess to html folder and it worked fine. Now no issue

      THANKS A TON
      {{ DiscussionBoard.errors[8980517].message }}
  • Profile picture of the author buysellbrowse
    Assuming you are running apache, in the server's conf file, there is a directive for ServerRoot, which in your case should point to the html subdirectory (i.e., ServerRoot "/var/www/html" ).
    Signature
    buysell-browse.com * Free Classifieds Advertising & Promotion *
    {{ DiscussionBoard.errors[9002218].message }}

Trending Topics