1

Help, please. Can you tell me if this is the correct code to perform the following rewrites? I can't test this particular change like I would like.

This company has four domains in a single apache instance.

I only want to affect one - test.example.com.

I need this block to do the following:

  1. if the host is test.example.com, any call to / needs to go to /admin/index.php
  2. if the host is test.example.com, any call to http needs to go to https
    
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} !^test\.example\.com
    RewriteRule ^/admin/index\.php$ - [L]

    RewriteCond %{HTTP_HOST} !^test\.example\.com
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /admin/index.php [L]

    RewriteCond %{HTTP_HOST} !^test\.example\.com
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
        

Thanks so much, Lynn

UPDATE:

How about something like this?

RewriteCond %{HTTP_HOST} !^test\.lifestylepubs\.com
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}$1
RewriteRule  ^/$ /admin/index.php
1
  • !^test\.example\.com (with a ! prefix) is a negated regex. So, this matches when the Host is not test.example.com, which would seem to be the opposite of what you were trying to achieve.
    – MrWhite
    Commented Oct 13, 2017 at 15:04

1 Answer 1

3

If your apache server serves 4 domains, then I would assume each domain is in its own vhost. If not, that's the first thing I would fix.

Then you can put your rewrite rule in the appropriate vhost configuration and not having to worry about much.

Otherwise your rewrite rules just need:

RewriteCond %{SERVER_NAME}       =test.example.com
RewriteRule  ^/$                 /admin/index.php [L]

RewriteCond %{SERVER_NAME}       =test.example.com        
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}$1
5
  • You're not wrong, and I don't disagree. Totally singing to the choir. Unfortunately the bosses dictating my schedule have set the priorities the way they are. I can't take the time right at this moment to do that, although I will asap. Today tho, I need to get test switched over to https.
    – Lynn Owens
    Commented Apr 15, 2013 at 19:40
  • I tried that, unfortunately it forced all domains to redirect to HTTPS, and in addition the URL ended up as prod.example.comprod.example.com
    – Lynn Owens
    Commented Apr 15, 2013 at 19:54
  • Maybe something like.... (see updated question)
    – Lynn Owens
    Commented Apr 15, 2013 at 19:58
  • See edit. I know that works as I use this myself somewhere.
    – ETL
    Commented Apr 15, 2013 at 21:10
  • RewriteCond %{SERVER_NAME} =test.example.com - You should ideally be testing against HTTP_HOST here, not SERVER_NAME (you are already using HTTP_HOST in the RewriteRule substitution). Whilst SERVER_NAME defaults to the same as HTTP_HOST, this is not necessarily the case. If UseCanonicalName On is set then SERVER_NAME is the value assigned to the ServerName directive, so this wouldn't work if test.example.com was a ServerAlias.
    – MrWhite
    Commented Oct 13, 2017 at 15:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .