用PHP编辑.htaccess

我有一个.htaccess将域映射到文件夹.

RewriteEngine On
RewriteBase /

# redirect mapped domain
ReWriteCond %{HTTP_HOST} joshblease.uk.to
ReWriteCond %{REQUEST_URI} !gme-index/
ReWriteRule ^(.*)$gme-index/$1 [L]

有没有办法使用PHP编辑/添加额外的域映射到文件

简单地说,我想获取.htaccess文件内容并使用PHP脚本添加到它们中.

解决方法:

正如上面的一条评论中所建议的那样,最好在这里使用RewriteMap而不是直接从PHP代码编辑.htaccess.以下是如何使用它的示例:

>将以下行添加到httpd.conf文件中:

RewriteMap domainMap txt://path/to/domain-dir.txt

>将文本文件创建为/path/to/domain-dir.txt,如下所示:

sub1 /subdir1
sub2 /foodir2
foo /bar

>在.htaccess文件添加以下行:

Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$[NC]
RewriteRule ^$${domainMap:%1} [L,R]

实际上,所有这些方法都是将这些重定向到位:

> sub1.domain.com/ => sub1.domain.com/subdir1
> sub2.domain.com/ => sub2.domain.com/foodir2
> foo.domain.com/ => foo.domain.com/bar

优点:有了这个设置,您可以根据需要编辑或重新创建文件/path/to/domain-dir.txt,而无需打开巨大的安全漏洞,允许PHP代码直接编辑.htaccess.

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...