简体   繁体   中英

Configuring .htaccess for main domain and subdomains using .htaccess only

I have a hosting with www.main.com based on Apache 2.4 and multiple subdomains like

www.sub1.main.com, www.sub2.main.com, ....

the folder structure is

/home/main/.htaccess + project files and subdomain folders like:
/home/public-html/main/sub1.main.com/.htaccess
/home/public-html/main/sub1/.htaccess + project1 files
/home/public-html/main/sub2/.htaccess + project2 files

etc.

subdomains are separate projects based on different technologies

The /home/main/ website based on AngularJS and has standard angular .httaccess configuration:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]

RewriteRule ^(.*) /index.html [NC,L]

I want to have another .htaccsess config in sub.main.com like this:

RewriteEngine On
RewriteRule ^$ http://127.0.0.1:3000/ [P,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^api$ http://127.0.0.1:3000/$1 [P,L]

Which should proxy redirect my Apache traffic to my nodejs server.

at some point my subdomain redirection is not working. This is only works from the main folder.

So I have 3 questions:

  1. Shall I configure all rules for all domains in the main .httaccess? (what is not really good due to potential url clash)

  2. Is it possible to configure main .htaccess just for allowing others sub.domains to use their own .httaccess

  3. where is the proper location for .httaccess subdomains?

here - /home/public-html/main/sub1.main.com/.htaccess or here - /home/public-html/main/sub1/.htaccess + project1 files

This is a shared hosting with limited access to apache configuration. I can use only .httaccess way, I don't have permissions to change my virtual hosts so please provide .httaccess solutions only.

Both code snippets above works and redirects no node as well but only from the home/main folder not from subdomains.

I need the special .htaccess per subdomain to host multiple projects

I found the solution for anyone who will want to use nodeJS on shared hosting:

put to /home/main/www/ .htaccess below:

 RewriteEngine on RewriteOptions inherit RewriteCond %{HTTP_HOST} ^main\\.org$ [OR] RewriteCond %{HTTP_HOST} ^www\\.main\\.org$ RewriteCond %{REQUEST_URI} !^/\\.well-known/acme-challenge/.+$ RewriteCond %{REQUEST_URI} !^/\\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$ RewriteCond %{REQUEST_URI} !^/\\.well-known/pki-validation/(?:\\ Ballot169)? RewriteCond %{REQUEST_URI} !^/\\.well-known/pki-validation/[A-F0-9]{32}\\.txt(?:\\ Comodo\\ DCV)?$ RewriteRule ^/?$ "http\\:\\/\\/angular\\.main\\.org\\/" [R=301,L] RewriteBase / RewriteCond %{REQUEST_URI} !^/node/ # Rewrites all URLS node.main to node server RewriteCond %{HTTP_HOST} ^(www\\.)?node.main\\. # Redirect to node server RewriteRule ^(.*)$ http://127.0.0.1:3000/$1 [P, L] 

Inside /home/main/www/angular put .htaccess like:

 RewriteEngine on RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^.*$ - [NC,L] RewriteRule ^(.*) /index.html [NC,L] 

and we don't need any .htaccess in the node folder I hope it helps to someone who have the same problem.

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