简体   繁体   English

使用PHP在Godaddy托管中创建子域

[英]Create a Subdomain in godaddy hosting using PHP

Is there any easiest way to create a sub domain using PHP code on godaddy.com hosting? 有什么最简单的方法可以在godaddy.com托管上使用PHP代码创建子域?

eg: 例如:

jon.mywebsite.com
jessie.mywebsite.com

etc 等等

you need to create a A record to serve all your subdomains 您需要创建一个A记录来服务您的所有子域

*.your-site.com       IN  A       YOUR-IP-ADDRESS

then you need to create a .htaccess file and 那么您需要创建一个.htaccess文件,

RewriteCond {REQUEST_URI} !\.(png|gif|jpg)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?uri=$1&hostName=%{HTTP_HOST}

this will ignore images (SEO friendly URLs). 这将忽略图像(SEO友好URL)。

Now you can redirect your users to $userName.your-site.com 现在,您可以将用户重定向到$ userName.your-site.com

Alternatively try this: 或者尝试以下方法:

setup your application so your users goes to your-site.com/user your .htaccess should look like this 设置您的应用程序,以便您的用户访问your-site.com/user。您的.htaccess应该如下所示

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([aA-zZ])$  index.php?username=$1
RewriteCond %{HTTP_HOST} ^(^.*)\.mywebsite.com
RewriteRule (.*)  index.php?username=%1

so now when you hit the index.php it will grab the user and redirect to $user.your-site.com as a custom subdomain. 因此,当您点击index.php时,它将捕获用户并重定向到$ user.your-site.com作为自定义子域。 (in this case usernames are limited to az characters) (在这种情况下,用户名仅限于z字符)

I dont think with PHP Code you can but with the help of .htaccess i think it is possible. 我不认为可以使用PHP代码,但是在.htaccess的帮助下,我认为这是可能的。

RewriteEngine On

RewriteCond %{THE_REQUEST} \ /+jon/
RewriteRule ^jon/(.*)$ http://jon.mywebsite.com/$1 [L,R=301]

RewriteCond %{THE_REQUEST} \ /+jessie/
RewriteRule ^jessie/(.*)$ http://jessie.mywebsite.com/$1 [L,R=301]

RewriteRule ^(jon|jessie)/ - [L]

RewriteCond %{HTTP_HOST} ^(www\.)?jon\.mywebsite\.com$ [NC]
RewriteRule ^(.*)$ /jon/$1 [L]

RewriteCond %{HTTP_HOST} ^(www\.)?jessie\.mywebsite\.com$ [NC]
RewriteRule ^(.*)$ /jessie/$1 [L]

You can make subdomain much easier using control panel, why to do it the hard way ? 您可以使用控制面板使子域变得更加轻松,为什么要采用困难的方式呢?

This is an old question, but in case anyone is looking for the answer. 这是一个古老的问题,但万一有人在寻找答案。 This is specific to GoDaddy, but other Hosts may do something similar to this. 这特定于GoDaddy,但其他主机可能会执行类似的操作。

After you setup your DNS A record you need to go to Hosted Domains and add the subdomain folder name under the root domain account. 设置DNS A记录后,您需要转到Hosted Domains并在根域帐户下添加子域文件夹名称。 Go to Hosted Domains. 转到托管域。 Under Hosted Domains you will see a root domain table column. 在“托管域”下,您将看到一个根域表列。 You will also see a Subdomains table column with an "Add" link. 您还将看到带有“添加”链接的“子域”表列。 This link is for adding subdomains to a root domain. 该链接用于将子域添加到根域。 Type in/enter the name of the subdomain folder. 输入/输入子域文件夹的名称。

Create either an index.php or index.html file and upload it to your subdomain folder and echo something like "Setup Completed". 创建一个index.php或index.html文件,并将其上传到您的子域文件夹,并回显类似“安装完成”的内容。 I noticed that if you are doing something like creating a restricted "api" subdomain for example and the GD script cannot access the index file in your subdomain folder then the "process" does not complete until you add a plain index.php or index.html file in your subdomain folder. 我注意到,如果您正在执行类似创建受限的“ api”子域之类的操作,并且GD脚本无法访问子域文件夹中的索引文件,则“过程”将无法完成,直到您添加简单的index.php或索引。子域文件夹中的html文件。 After that is done then you can add anything you want in that subdomain folder/site. 完成之后,您可以在该子域文件夹/站点中添加所需的任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM