OpenShift bind multiple domains

Server Norckon 288℃ 0评论

1476961369-8108-maxresdefault

INTRODUCE

OpenShift default using the Apache web server and user cannot modify the virtual host configuration file. Is there any way to allow OpenShift support multiple domains? The answer is yes. You can just only using Apache htaccess rule to get multiple domain supports.

STEP BY STEP

1. BIND YOUR DOMAINS OR SUB-DOMAINS TO OPENSHIFT

First please bind your domains or your sub-domains to your OpenShift cloud host. For example, I have 3 sub-domains and a root domain need bind to OpenShift. I did those changes in my domain control panel.

1476961332-9254-dns

2. ADDING YOUR DOMAINS OR SUB-DOMAINS IN OPENSHIFT

After binding domains, If you want access OpenShift from this domain, you need add your domain to OpenShift Cloud Host control panel. Just for example:

1476961332-8574-alias

3. CREATE A HTACCESS FILE IN WEBROOT

Create a .htaccess file in your OpenShift web-root, and puts domain and directory relationship in it. Before do that please confirm those directory is in your web-root, if not please create it first.

The sample .htaccess file:

RewriteEngine On
#----------BEGIN----------
DirectoryIndex index.php index.html index.htm
# Redirect Top-domain to www sub-domains
RewriteCond %{HTTP_HOST} ^raineggplant\.com$ [NC]
RewriteRule ^(.*)$ http://www.raineggplant.com$1 [R=301,L]

# Directory of sub-domain: www
RewriteCond %{HTTP_HOST} ^www\.raineggplant\.com$ [NC]
RewriteRule ^(.*)$ /www/$1 [L]

# Directory of sub-domain: bbs
RewriteCond %{HTTP_HOST} ^bbs\.raineggplant\.com$ [NC]
RewriteRule ^(.*)$ /bbs/$1 [L]

# Directory of sub-domain: app
RewriteCond %{HTTP_HOST} app\.raineggplant\.com$ [NC]
RewriteRule ^(.*)$ /app/$1 [L]
#----------END----------

4. CREATE HTACCESS FILE IN DIRECTORY OF EACH DOMAIN

Create a .htaccess file in directory of each domain or sub-domain for let which known and accept access from domains.

The sample .htaccess file:

RewriteEngine On
RewriteBase /app/

5. SOLVE REDIRECT WRONG OF SUCH /APP/APP REQUESTION

After step 1 to 4, You just can access your website via other domains or sub-domains, but there a problem.

Just like the sub-domain app has redirected to directory app, but there still have an app directory in the app. If you access from http://app.example.com/app will redirect to http://app.example.com.

Following code can solve this problem:

# Get prefix of domain, and save to %{ENV:sdn}
RewriteCond %{HTTP_HOST} ^(.*)\.raineggplant\.com$ [NC]
RewriteRule ^(.*)$ - [E=sdn:%1]

# If domain is app.raineggplant.com
RewriteCond %{HTTP_HOST} ^app\.raineggplant\.com$ [NC]
RewriteCond %{THE_REQUEST} ^[ACDEGHILNOPRST]+\ /app/
RewriteCond %{REQUEST_URI} !^/app/app/
RewriteRule ^(.*)$ /app/app/$1 [L]

# If domain is not app.raineggplant.com
RewriteCond %{HTTP_HOST} !^app\.raineggplant\.com$ [NC]
RewriteCond %{THE_REQUEST} ^[ACDEGHILNOPRST]+\ /app/
RewriteCond %{REQUEST_URI} !^/%{ENV:sdn}/app/
RewriteRule ^(.*)$ /%{ENV:sdn}/app/$1 [L]

6. SOLVE REDIRECT WRONG OF / MISSING IN THE END OF URL

If you do a request such as http://app.example.com/dir1, will not redirect to dir1 in app but app root will be redirected. Replacing following code:

RewriteCond %{HTTP_HOST} ^app\.raineggplant\.com$ [NC]
RewriteCond %{THE_REQUEST} ^[ACDEGHILNOPRST]+\ /app/
RewriteCond %{REQUEST_URI} !^/app/app/
RewriteRule ^(.*)$ /app/app/$1 [L]

to:

# if domain is app.raineggplant.com
RewriteCond %{HTTP_HOST} ^app\.raineggplant\.com$ [NC]
RewriteCond %{THE_REQUEST} ^[ACDEGHILNOPRST]+\ /app/
RewriteCond %{REQUEST_URI} !^/app/app/|^/app/qr/
RewriteRule ^(.*)$ /app/app/$1 [L]

# Do a process
RewriteCond %{HTTP_HOST} ^app\.raineggplant\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/app/qr/
RewriteCond %{THE_REQUEST} ^[ACDEGHILNOPRST]+\ /app/
RewriteRule ^(.*)$ http://app.raineggplant.com/$1 [L]

will solve this problem.

REFERENCE

OpenShift添加子域名子站教程(二).htaccess文件法

TRANSLATE

Pekaikon Norckon
2016-10-10

喜欢 (1)
COMMENT
Cancel Comment

EMOJI

Hi, We need some information

  • Nickname (*)
  • E-Mail (*)
  • Website