Tag Archives: rewrite

Apache setting and reading Environmental Variables

A common .htaccess issue: you need one for production and another for development.

Here’s a simple trick to set and read apache env vars.

1
2
3
4
5
6
7
8
9
10
11
12
13
<IfModule mod_rewrite.c>
  RewriteEngine On

  # do not force https on local environment
  RewriteCond %{SERVER_NAME} local.yoursite.net
  RewriteRule .? - [E=siteenv:local]

  RewriteCond %{HTTP:X-Forwarded-Proto} !https # not on https
  RewriteCond %{ENV:siteenv} !local # not on local environment
  RewriteRule !/status https://%{SERVER_NAME}%{REQUEST_URI} [L,R]

  ...
</IfModule>

On this example we do not force HTTP when accessing via “local.yoursite.net” (a development environment)

nginx serve subdirectory as domain root

Say you have the following file structure:

1
2
3
4
5
site.com/
  main_site/
  site_a/
  site_b/
  site_c/

If you want:

  • http://site.com to show /main_site/’s files
  • all remaining sites to be served as usual (example: http://site.com/site_b/)

All you have to do is to edit site.com.conf’s file as follows:

1
2
3
4
5
6
7
8
9
10
11
  location / {
    try_files $uri $uri/ @missing; # try to get site.com/requested_file, if does not exist jump to @missing
  }

  location /main_site {
    # do nothing
  }

  location @missing { # if file is not present on the root, serve the /main_site
    rewrite ^ /main_site$request_uri?;
  }

Source