Search code examples
apache.htaccesshttp-redirectsubdomainno-www

Making Exception in Htaccess for staging site


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)?


Solution

  • 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