二级域名绑定子目录

通过配置.htaccess文件实现子目录绑定二级域名的方法

比方说我有个顶级(一级)域名:dshvv.com ,有一台服务器centos7的。我如何让yh.dshvv.com访问到另一个网站信息呢,注意哦,我只有一台服务器和一个顶级域名。这就是二级域名技术了

第一步:安装apche服务器

yum install httpd

截至CentOS7, mod_rewrite Apache模块默认情况下启用。 我们会验证这是与案件httpd -M标志,打印所有加载的模块列表

第二步:创建.htaccess文件

在/var/www/html目录下,创建.htaccess在默认文档根目录

touch /var/www/html/.htaccess

第三步:子目录绑定二级域名

编辑.htaccess文件,新增如下内容

# 开启功能 
RewriteEngine on 

# 你要绑定的二级域名 
RewriteCond %{HTTP_HOST} ^(yh.)?dshvv.com$ 

# 把那个子目录指向要绑定的二级域名 
# 这里以子目录blog目录为例 
RewriteCond %{REQUEST_URI} !^/yh/ 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ /cms/$1 

#这里改成要绑定的二级域名和要绑定的子目录 
RewriteCond %{HTTP_HOST} ^(yh.)?dshvv.com$ 
RewriteRule ^(/)?$ yh/index.html [L]

当然,网上还有很多其它的写法,下边一种也比较简单,而且是配置了两个二级域名

# 开启功能 
RewriteEngine on 

# 第一个二级域名
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^wx.dshvv.com$
RewriteCond %{REQUEST_URI} !^/wx/
RewriteRule ^(.*)$ /wx/$1

#第二个二级域名
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^yh.dshvv.com$
RewriteCond %{REQUEST_URI} !^/yh/
RewriteRule ^(.*)$ /yh/$1

第四步:设置.htaccess文件生效

在/etc/httpd/conf/httpd.conf目录下,修改
找到部分,然后更改AllowOverride从指令None到All :

<Directory /var/www/html>
. . .
 # 
 # AllowOverride controls what directives may be placed in .htaccess files.
 # It can be "All", "None", or any combination of the keywords:
 # Options FileInfo AuthConfig Limit
 #
 AllowOverride All
. . .
</Directory>

然后重启apche就好了,参考:
https://www.howtoing.com/how-to-set-up-mod-rewrite-for-apache-on-centos-7/
https://www.jb51.net/article/22037.htm

转载于:https://www.cnblogs.com/dshvv/p/11163323.html

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用二级域名访问 Flask 中的文章,你需要在 DNS 中设置二级域名解析,并且在 Flask 应用程序中设置路由。 首先,在 DNS 中添加一个 CNAME 记录,将二级域名指向你的服务器 IP 地址,例如 subdomain.yourdomain.com 指向 123.45.67.89。 然后,在 Flask 应用程序中设置一个路由,用于处理访问该二级域名的请求。你可以使用 Flask Blueprint 实现该功能。如下所示: ```python from flask import Blueprint, render_template subdomain_bp = Blueprint('subdomain', __name__, subdomain='<subdomain>') @subdomain_bp.route('/') def index(subdomain): # 在此处处理与该二级域名相关的请求 # 返回渲染的模板或响应 return render_template('subdomain_index.html') ``` 上述代码中,`subdomain_bp` 是一个 Blueprint 对象,它包含一个 `subdomain` 参数,这个参数可以从请求 URL 中的子域名中获取。然后,你可以在路由方法 `index` 中处理该二级域名相关的请求并返回相应的响应或模板。 最后,将 Blueprint 对象注册到 Flask 应用程序中: ```python from flask import Flask app = Flask(__name__) app.register_blueprint(subdomain_bp, subdomain='<subdomain>') ``` 在上述代码中,将 Blueprint 对象 `subdomain_bp` 注册到应用程序中,并指定子域名参数的名称为 `subdomain`。 现在,当访问该二级域名时,Flask 将使用上述路由方法来处理请求并返回响应。需要注意的是,为了访问该二级域名,你需要先设置 DNS 解析,然后才能在浏览器中通过二级域名访问 Flask 应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值