1

I made a staging wordpress site (test environment)- www.staging.example.com alongside the core site -www.example.com. problem is the main site www.example.com is tied to SSL. Hence, I cant get to staging site. The Htaccess inside the main site is mentioned below. It redirects every non-www To www and then http TO https in turn. Can I just add an exception here to my staging site- www.staging.example.com (root dir: public_html/staging/). I also want to allow only my own ip to this staging site so that no one else can have access.

<IfModule mod_rewrite.c>
#First rewrite any request to the wrong domain to use the correct one (here www.)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

#Now, rewrite to HTTPS:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

I also want to know if I need to put anything in the Htaccess of my staging site (public_html/staging/htaccess)?

Tanvir
  • 1,642
  • 8
  • 32
  • 53

1 Answers1

1

Change you root .htaccess to this:

RewriteEngine On

# add www and http->https except for staging site    
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^(?:www\.)?staging\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

Inside public_html/staging/.htacces have this line to allow only your IP to access:

Order Deny,Allow
Deny from all
Allow from 10.168.0.101
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    Man u r a life savior ,Gracious !! Exactly what I wanted. Now that I made the staging site accessible without SSL and with my own IP. I added a robots.txt with this, so that search engine doesnt crawl- User-agent: * Disallow: / Is there anything I should do for a staging site? Thanx a LOT. – Tanvir May 25 '17 at 10:39
  • Hello, can u plz change the above htaccess, so that at first it redirects all http > https, then non-www > www. (All the other condition will remain the same for staging site) I m trying to add HSTS preload. Hence it says- " http : // mydomain.com (HTTP) should immediately redirect to https : // mydomain.com (HTTPS) before adding the www subdomain. Right now, the first redirect is to https :// www.mydomain.com. The extra redirect is required to ensure that any browser which supports HSTS will record the HSTS entry for the top level domain, not just the subdomain." – Tanvir Jun 03 '17 at 12:40
  • Actually, just changing the .htaccess as I requested above wud do my job for now. Plz Change the above htaccess, so that at first it redirects all http > https, then non-www > www. (All the other condition will remain the same for staging site) . At present it first redirects all non-www > www, then http>https. Thanx – Tanvir Jun 04 '17 at 05:04
  • HSTS is a header which needs to be in place when you use SSL in your site. I myself know a little about this. – Tanvir Jun 04 '17 at 05:05
  • but it seems , the non-www > www occurs first, then http>https. When it does both , first it redirects non-www > www , then http>https. I just want to change that order. – Tanvir Jun 04 '17 at 11:46
  • I find that in ur rewrite rule above , the http>htttps comes at the end, cant u just put it at the beginning someway !! So that the redirection to https occurs at the beginning. – Tanvir Jun 05 '17 at 03:31
  • but if u check my original question above, the htaccess I posted above has 2 sections (so I guess) in it, first it redirects all non-www> www, then all http>https. U then just added the exception for staging site. – Tanvir Jun 05 '17 at 05:20
  • hello, bro , I was using ur rule for a long time, suddenly for some reasons, I cant access the staging site - www.staging.example.com. the first rule on .htaccess is still as u mentioned above. I uploaded a simple script on staging domain. But I find this error- server DNS address could not be found. ERR_NAME_NOT_RESOLVED. What can be the problem ? – Tanvir Jul 24 '17 at 17:11
  • `ERR_NAME_NOT_RESOLVED` error is usually not related with `mod_rewrite`. Even if you rename this .htaccess this error may still come. – anubhava Jul 24 '17 at 17:58
  • 1
    Thnk u very much, the problem is with the Cloudfare server, I had to add a CNAME for my subdomain, now its workiing well, Thank u very much again – Tanvir Jul 24 '17 at 18:48
  • bro I have added an ADD-ON Domain to my main domain , how can exclude that domain in my htacces ABOVE. I want to avoid all Http > Https for www.mytest.site. I cant access the site , I think the problem is this Htaccess file. I need exception rule for www.mytest.site Thnk u in advance – Tanvir Jul 31 '17 at 11:23
  • Change above condition line to: `RewriteCond %{HTTP_HOST} !^(?:www\.)?(?:staging|mytest)\. [NC]` – anubhava Jul 31 '17 at 11:35