问题
I have a project where users can create their own profiles. And the profiles will have sub-domain URLs like robert.blogger.com. So if that user has some domain of his own like robert.com.
Then I want every request for robert.com to redirect to robert.blogger.com without changing the URL.
The URL should show robert.com/home.html, robert.com/aboutus.html etc. but actually code should be run from robert.blogger.com/index.html, robert.com/aboutus.html etc.
Is this possible? If so, how can this be done?
回答1:
This is not done with php.
The best way to accomplish this is via an HTACCESS redirect. Alternatively this can be done via a DNS A record, but would require elevated access.
place a file named (.htaccess) in the root directory of the domain that contains the following:
RewriteEngine On
RewriteRule * subdomain.domain.com [L]
回答2:
Not possible in php unless you use curl. You need to setup a wildcard DNS record to catch all subdomains and send them to you web server. Then you need to setup Apache to catch all named virtual hosts and send it to a directory.
回答3:
I would just the header function. header("Location: http://"$user_name."blogger.com");
回答4:
This will work
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domainname.com
RewriteCond %{HTTP_HOST} ([^.]+)\.domainname.com
RewriteRule ^(.*) user/user.php?username=%1
ErrorDocument 404 /notfound.php
IndexIgnore .htaccess */.??* *~ *# */HEADER* */README* */_vti*
来源:https://stackoverflow.com/questions/20858375/redirecting-to-a-subdomain-from-another-domain-without-changing-its-url