htaccess redirect www to non-www, htaccess redirect non-www to www

Home Forums BulletProof Security Pro htaccess redirect www to non-www, htaccess redirect non-www to www

Viewing 15 posts - 1 through 15 (of 19 total)
  • Author
    Posts
  • #1720
    AITpro Admin
    Keymaster

    Email Question:

    Note:  actual domain name has been replaced with example.com for privacy

    Hi,

    I have visited the page, and I have tried to do some changes with htaccess file.

    I  am trying several times to rewrite www to my domain but it does not working. I have insert this code to htaccess (public_html/f4):

    #Rewrite to www
    Options +FollowSymLinks
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^example.com.my[nc]
    RewriteRule ^(.*)$ http://www.example.com.my/$1 [r=301,nc]
    

    Please help us to rewrite. The current setting for our web are:

    1. public_html/f4 (main site -wordpress)*if user type www.example.com.my or example.com.my should redirect topublic_html/f4/index.php and the url address will have www at the domain name.

    2. if user type: www.example.com.my/autoville it should redirect to http://example.com.my/f4/project-emkay/autoville-2/autoville-overview/

    3. if user type: www.example.com.my/affordablehomes it should redirect to http://example.com.my/f4/register-your-interest/

    * the #2 and #3 are working but, it remove the www

    Code from attached htaccess file:

    RewriteCond %{HTTP_HOST} ^example\.com\.my$ [OR]
    RewriteCond %{HTTP_HOST} ^www\.example\.com\.my$
    RewriteRule ^autoville$ "http\:\/\/example\.com\.my\/f4\/project\-emkay\/autoville\-2\/autoville\-overview\/" [R=301,L]
    RewriteCond %{HTTP_HOST} ^example\.com\.my$ [OR]
    RewriteCond %{HTTP_HOST} ^www\.example\.com\.my$
    RewriteRule ^affordablehomes$ "http\:\/\/example\.com\.my\/f4\/register\-your\-interest\/" [R=301,L]
    

    Please advice
    Thanks in advance.
    Regards
    Nabil

    #1723
    AITpro Admin
    Keymaster

    Ok first off here are some basic examples of how to Rewrite non-www to www and www to non-www for any folks who are looking for how to do this.  I will answer your questions specifically after showing these basic examples.  What is important to note is that this code needs to be incorporated/included in the WordPress htaccess Rewrite Loop.  In your Root .htaccess file you will see this code.  This code is the beginning/start of the WordPress Rewrite loop.

    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    

    You would incorporate/include your non-www to www or www to non-www Rewrite htaccess code in the beginning of the WordPress Rewrite loop as shown below.

    # Rewrite non-www to www
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
    RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
    RewriteRule ^index\.php$ - [L]
    
    # Rewrite www to non-www
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
    RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
    RewriteRule ^index\.php$ - [L]
    

    Ok now to answer your question specifically.

    You are overcomplicating the rewriting and can just use a much simpler permanent redirect as shown below.  Note:  you would be including “www” in the redirect path. Note:  You would not use a back reference $1 at the end of the redirect path because you are not trying to splice the URL and want a full redirect instead to a new URL.

    RedirectMatch 301 ^/autoville$ http://www.example.com.my/f4/project-emkay/autoville-2/autoville-overview/
    
    RedirectMatch 301 ^/affordablehomes$ http://www.example.com.my/f4/register-your-interest/

    For BPS and BPS Pro specifically you would do these steps to add whichever Rewrite/Redirect code you want to use and save it to BPS Custom Code:
    1. Copy whichever code you want to use to this BPS Root Custom Code text box: CUSTOM CODE WP REWRITE LOOP START and replace example.com with your actual website domain name.
    2. Click the Save Root Custom Code button to save your Root custom code.
    3. Go to the BPS Security Modes page and click the Root Folder BulletProof Mode Activate button.

    # WP REWRITE LOOP START
    # Rewrite non-www to www
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
    RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
    RewriteRule ^index\.php$ - [L]
    # WP REWRITE LOOP START
    # Rewrite www to non-www
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
    RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
    RewriteRule ^index\.php$ - [L]
    #4818
    J Garner
    Participant

    Hi,

    Can you explain if it is possible to preserve the code required for this by using the BPS Pro “Custom code” system or does it have to be done via the “htaccess File Editor” tab?

    Also I have a site with an exisiting domain 1) that I’d like to have the www version redirecting to the nonwww version of the domain but also add 2 other domains 2) and 3) with both their www and nonwww versions pointing to domain 1)

    Is it possible to use the custom code function and what would the required code be to accomplish the above regardless?

    John

    #4819
    AITpro Admin
    Keymaster

    You would be modifying the standard BPS root .htaccess file code via the built-in htaccess editor – htaccess File Editor tab page.  We are planning on adding 2 new additional Custom Code text boxes for:  1 for the HEAD nuisance filter and 1 for the entire Query String Exploits filter section of code. BPS Pro upgrades do not change or modify any custom code additions or modifications that you have made directly to your .htaccess files.  BPS has built-in htaccess file backup and restore to make backup copies of your .htaccess files.  We may decide to add an additional Custom Code text box for the top section of code that you are referring to, but at this point only 2 new Custom Code text boxes are planned.

    Check your WordPress General Settings first to make sure the URL settings are what you want (in your case non-www) then….

    For domain1 – rewrite www to non-www

    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
    RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
    RewriteRule ^index\.php$ - [L]

    For the other domains you would add the code to each site’s root .htaccess file to redirect to whatever site you want to redirect too.

    http://forum.ait-pro.com/forums/topic/html-htaccess-redirect-code/#post-3437

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www.olddomain.com$
    RewriteRule (.*)$ http://www.newdomain.com/$1 [R=301,L]
    #5492
    AITpro Admin
    Keymaster

    If you have a Network/Multisite WordPress installation see this post we created in the link below to add www to non-www redirect code for WordPress Network/Multisite.

    http://wordpress.org/support/topic/where-to-add-301-redirects/page/2#post-4174407

    #5535
    J Garner
    Participant

    I have messed things up on a dev server (so not critical !) I had a plugin blocking access to the site (called Ultimate Coming Soon Page) but deactivated it and I had the code:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www.domainname.com$ [NC]
    RewriteRule ^(.*)$ http://domainname.com/$1 [L,R=301]

    in custom code in “CUSTOM CODE TOP” I then saved and then get in the htaccess file:

    ServerSignature Off
    
    # ADD A PHP HANDLER
    # If you are using a PHP Handler add your web hosts PHP Handler below
    
    # CUSTOM CODE TOP
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www.domainname.com$ [NC]
    RewriteRule ^(.*)$ http://domainname.com/$1 [L,R=301]
    
    # DO NOT SHOW DIRECTORY LISTING
    # If you are getting 500 Errors when activating BPS then comment out Options -Indexes
    # by adding a # sign in front of it. If there is a typo anywhere in this file you will also see 500 errors.
    Options -Indexes

    I then activated bulletproof modes.

    However I was then redirected from the www vers site to a login page on the nonwww vers site and from there when I entered my username and password I get a message saying it was the wrong one. I didn’t know whether this was Login security flexing its muscles but couldn’t figure out why my username was/is being rejected

    Also on the main page I am getting a
    Error 310 (net::ERR_TOO_MANY_REDIRECTS): There were too many redirects.

    The .htacess file is locked and chmoded to view only so I’m stuck. I guess I’m going to have to ssh in and delete the htaccess file. Any ideas what I did wrong / what went wrong?
    Thanks,
    John

    #5536
    AITpro Admin
    Keymaster

    You actually need to incorporate/add your www to non-www code directly into your root .htaccess file and should not use Custom Code.

    Use the BPS .htaccess editor and edit your root .htaccess file directly.  Click this link to jump to the post in this thread to see examples.

    http://forum.ait-pro.com/forums/topic/htaccess-redirect-www-to-non-www-htaccess-redirect-non-www-to-www/#post-1723

     

    #5554
    AITpro Admin
    Keymaster

    You are combining the code into the existing code and NOT adding a new block of code.

    You are only adding these 2 lines of code.

    RewriteCond %{HTTP_HOST} ^www\.domainname\.com$ [NC]
    RewriteRule ^(.*)$ http://domainname.com/$1 [R=301,L]

    To the existing code in your root .htaccess file and NOT creating a new block of code.

    Click this link for an example:  http://forum.ait-pro.com/forums/topic/htaccess-redirect-www-to-non-www-htaccess-redirect-non-www-to-www/#post-1723

    Yes, I try to tell people to check their WordPress Settings first, but that usually goes nowhere. 😉

    You should never ever have to uninstall BPS Pro.  If you run into problems not being able to log back into your site because of a bad .htaccess file then you would just FTP to your website and delete your Root .htaccess file.

    For all other issues/problems/troubleshooting BPS Pro has built-in Troubleshooting capability.  There is an On/Off option for every single feature in BPS Pro.  You actually do not ever need to deactivate the BPS Pro plugin either for the same reason.

    http://forum.ait-pro.com/forums/topic/read-me-first-pro/#bps-pro-general-troubleshooting

    If your domain name/domain URL changes then yes you will have to request a new Activation Key.  Changing from www to non-www or vice versa or changing from http to https or vice versa is changing your domain name/domain URL.  You do not need to uninstall BPS Pro in order to request a new Activation Key.

    #5564
    J Garner
    Participant

    I have just several glasses of wine, so not as sharp as usual, hence the following questions 😉

    – I don’t understand why it’s not good to use the custome code top, since it gets inserted right at the top when I look at the resulting htaccess file.

    – When you talk about inserting it to the existing code I guess that would be using:
    From the left menu, the BPS Pro > htaccess File Editor
    From the top menu Edit/Upload/Download > secure .htaccess

    – Does it matter where I insert the code and if so where should I insert the redirect code (www to nonwww), before BEGIN WORDPRESS?

    And isn’t this going to get overwritten if I add anything to the CUSTOM CODE section and recreate the secure and default .htaccess files

    Yes since I changed the URL I guessed that was why I was just getting a grey bar below the error messages and none of the Bullet Proof configuration pages, so I re-installed it 🙂

    #5566
    AITpro Admin
    Keymaster

    UPDATE  New Custom Code instructions:

    1. Copy the WP REWRITE LOOP START .htaccess code below to this Custom Code text box:  CUSTOM CODE WP REWRITE LOOP START
    2. Click the Save Root Custom Code button.
    3. Go to the BPS Security Modes page and click the Root Folder BulletProof Mode Activate button.

    IMPORTANT NOTE: If your WordPress site is installed in a subfolder then add the folder name to the RewriteBase line of code. Example: WordPress is installed in a folder named “myblog”. The RewriteBase would be RewriteBase /myblog/.

    www to non-www

    # WP REWRITE LOOP START
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^www\.domainname\.com$ [NC]
    RewriteRule ^(.*)$ http://domainname.com/$1 [R=301,L]
    RewriteRule ^index\.php$ - [L]

    non-www to www

    # WP REWRITE LOOP START
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^domainname\.com$ [NC]
    RewriteRule ^(.*)$ http://www.domainname.com/$1 [R=301,L]
    RewriteRule ^index\.php$ - [L]

    ___________________________________

    The secure.htaccess tab is your Root .htaccess Master File.
    Your Current Root htaccess File tab is your currently active root .htaccess file.

    Yes, it absolutely matters where you add your www to non-www code.

    Click on the Your Current Root htaccess File tab and scroll down in the contents of your Root .htaccess file until you see this htaccess code.

    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]

    Then you are going to add your www to non-www code into the existing code above and combine it into the existing code.

    Your www to non-www code.

    RewriteCond %{HTTP_HOST} ^www\.domainname\.com$ [NC]
    RewriteRule ^(.*)$ http://domainname.com/$1 [R=301,L]

    The combined end result of adding your www to non-www code to the existing root .htaccess file is this below.

    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^www\.domainname\.com$ [NC]
    RewriteRule ^(.*)$ http://domainname.com/$1 [R=301,L]
    RewriteRule ^index\.php$ - [L]

    Yes,  at this point your direct edits to your root .htaccess code would be overwritten by Activating BulletProof Mode again.  We will be adding additional Custom Code text boxes for all areas of the root .htaccess file, but we seeing a lot of user mistakes in testing trials.  Creating the additional Custom Code text boxes is simple.  Predicting all the mistakes that people are going to make is the challenging part, which has pushed adding additional Custom Code text boxes back 8 months.

    #20765
    Pranay Rana
    Participant

    [Spam Post Allowed]
    I’m also trying to redirect the non-www version of my site to the www one, I have already set the www in the general settings of WordPress, but some of my friends suggested me to do a 301 redirection using .htaccess. If anyone could provide me with the exact code to perform the redirection through htaccess, that’d be great. Btw my site is: http://www.showbox-app.com

    #23964
    Pooja Verma
    Participant

    [Spam Post Allowed]
    Thanks for providing htaccess code. I have used given code to redirect http://dogandogs.com on my latest blog http://showboxdownloadapp.com.
    Thank you so much! Team

    #24359
    ali
    Participant

    [Spam Post Allowed]
    even i redirected my showbox apk http://www.showboxapkdownload.org download site to http://www.showboxapkdownload.org

    #28160
    simon
    Participant

    [Topic has been merged into this relevant Topic]
    Hi have a problem with a website.  www.christine-salopek.de  i get a blank page.  christine-salopek.de is working fine and www.christine-salopek.de/biografie/ is also redirecting as it should! only the frontpage without the www prefix does not work. The hoster 1&1 checked everything and could not find a problem on the server side.

    Disabling plugins, saving permalinks and clearing cache did no solve the problem.

    W3 total cache gives me this message:
    The required directives for fancy permalinks could not be detected, please confirm they are available:
    http://codex.wordpress.org/Using_Permalinks#Creating_and_editing_.28.htaccess.29
    Could it be a BPS thing or do you have any idea?
    ( Please delete links)
    Thanks Simon

    #28165
    AITpro Admin
    Keymaster

    @ simon – See the examles in this reply above:  http://forum.ait-pro.com/forums/topic/htaccess-redirect-www-to-non-www-htaccess-redirect-non-www-to-www/#post-5566  You can disgegard the W3TC error – it is incorrect/invalid.

Viewing 15 posts - 1 through 15 (of 19 total)
  • You must be logged in to reply to this topic.