Redirecting to a subdomain from another domain without changing its URL

旧城冷巷雨未停 提交于 2019-12-13 06:23:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!