简体   繁体   中英

Apache rewrite of subdomain based on environment

I'm trying to come up with a rewrite rule that will work in conf files for all the environments I support (qa, stage, dev, etc), so a common rewrite condition and rule to "rule" them all.

Example:

  • subdomain.environment.site.com should redirect to environment.site.com
  • except subdomain.site.com should redirect to www.site.com.
  • It also shouldn't forward when some of the request URIs have stuff in them.

This is as far as I've come:

    RewriteCond %{HTTP_HOST}   subdomain.$ENV.site.com [NC]
    RewriteCond %{REQUEST_URI} !/images
    RewriteCond %{REQUEST_URI} !/blah
    RewriteCond %{REQUEST_URI} !/blah-foo
    RewriteCond %{REQUEST_URI} !/foo-bar
    RewriteRule ^/(.*)         http://$ENV.site.com/$1 [L,R=302]

Obvious the $ENV isn't going to work, but I am having a having a helluva time trying to find a proper regex to make this work in all environments.

A little more research, hand banging and beer got me the solution.

It might not be pretty but it works. Answering so if anyone ever tries to do this, they have some sort of start here.

    RewriteCond %{HTTP_HOST}   ^(sub)(\.)((?:[a-z][a-z]+))(\.)(site)(\.)(com) [NC]
    RewriteCond %{REQUEST_URI} !/image
    RewriteCond %{REQUEST_URI} !/blah
    RewriteCond %{REQUEST_URI} !/foobar
    RewriteCond %{REQUEST_URI} !/barfoo
    RewriteRule ^/(.*)         http://%3.%5.%7/$1 [L,R=302]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM