htaccess subdomain pointing

2020-04-16 18:05发布

I need some help on the htaccess subdomain to be pointed to specific file in a folder.

Inside the parent folder should contain individual php files to correctly path the right applications. Sounds simple here.

The url that the user has type should not change / redirect, instead it should only "link" to the correct file at the background

Example when user type userbase.company.com it should point to

http://company.com/parent/userbase.php

Similarly, when user type userbase2.company.com it should point to

http://company.com/parent/userbase2.php

but no redirection should happen. url links should remain at

http://userbase2.company.com 

folder would be the same, but not the file. Is it possible?

Currently I have this htaccess code:

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^userbase.company.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.userbase.company.com$
RewriteRule ^(.*)$ \./parent/userbase.php/$1 [L]

I'm going have to put this at /public_folder/ right? Not at the /public_folder/userbase ?

1条回答
神经病院院长
2楼-- · 2020-04-16 18:54

This rule should do the job:

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_URI} !^/parent/ 
RewriteCond %{HTTP_HOST} !=domain.com
RewriteCond %{HTTP_HOST} !=www.domain.com
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com$
RewriteRule (.*) /parent/%1.php%{REQUEST_URI} [L]
  1. URL will remain unchanged in browser (means rewrite -- internal redirect).

  2. It will only work for something.domain.com (not domain.com or www.domain.com).

  3. It will rewrite this request http://user1.company.com/hello/pink/kitten.php into /parent/user1.php/hello/pink/kitten.php

  4. If subdomain is complex (e.g. www.something.domain.com) then it will be reflected in rewrite as well: e.g. http://www.something.company.com/hello/pink/kitten.php will be rewritten to /parent/www.something.php/hello/pink/kitten.php. If such behaviour is undesired (I do not know your requirements for 100%) then you will need add 1 more condition to the rule.

  5. Request for a home page (e.g. http://user1.company.com/) will be rewritten in this way: /parent/user1.php/

P.S. Rule was tested before posting -- works fine, but you have to check and maybe tweak it if it needs to be working with your CodeIgniter app (sorry, I'm not familiar with that framework).

In any case this rule should be above your CodeIgniter rewrite rules (if there are such).

查看更多
登录 后发表回答