简体   繁体   English

用PHP编辑.htaccess

[英]Edit .htaccess with PHP

I have a .htaccess that maps a domain to a folder. 我有一个.htaccess将域映射到文件夹。

RewriteEngine On
RewriteBase /

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

Is there any way to edit/add extra domain maps to the file using PHP? 有没有办法使用PHP编辑/添加额外的域映射到文件?

Simply, I want to get the contents of the .htaccess file and add to them using a PHP script. 简单地说,我想获取.htaccess文件的内容并使用PHP脚本添加到它们中。

As suggested in one of the comments above it is better to use RewriteMap for your case here rather than trying to edit .htaccess from PHP code directly. 正如上面的一条评论中所建议的那样,最好在这里使用RewriteMap ,而不是直接尝试从PHP代码编辑.htaccess。 Here is a sample how to use it: 以下是如何使用它的示例:

  1. Add following line to your httpd.conf file: 将以下行添加到httpd.conf文件中:

     RewriteMap domainMap txt://path/to/domain-dir.txt 
  2. Create a text file as /path/to/domain-dir.txt like this: 像这样创建一个文本文件/path/to/domain-dir.txt

     sub1 /subdir1 sub2 /foodir2 foo /bar 
  3. Add these line in your .htaccess file: 在.htaccess文件中添加以下行:

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

Effectively all this means is to have these redirects in place: 实际上,所有这些方法都是将这些重定向到位:

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

Advantage: With this setup in place, you can edit or recreate the file /path/to/domain-dir.txt as much as you want from your php code without opening a huge security hole be allowing php code o edit .htaccess directly. 优点:有了这个设置,您可以根据需要编辑或重新创建文件/path/to/domain-dir.txt ,而无需打开巨大的安全漏洞,允许php代码直接编辑.htaccess。

This could work for your situation: 这可能适用于您的情况:

Ammend the .htaccess to look the following: 修改.htaccess以查看以下内容:

RewriteEngine On
RewriteBase /

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

###CUSTOM RULES###

php script assuming $rules holds the new generated rules to be ammended; PHP脚本假设$规则保持新生成的规则被修改;

$htaccess = file_get_contents('/path/to/.htaccess');
$htaccess = str_replace('###CUSTOM RULES###', $rules."\n###CUSTOM RULES###", $htaccess);
file_put_contents('/path/to/.htaccess', $htaccess);

example above is theory and has not been tested, would be dependant upon .htaccess privledges and permissions of the script. 上面的例子是理论,并且尚未经过测试,将依赖于.htaccess权限和脚本的权限。

You could use the following htaccess to map any domain other than maindomain.com to a folder that has the same name as the domain-name. 您可以使用以下htaccess将除maindomain.com之外的任何域maindomain.com到与域名同名的文件夹。

RewriteCond %{ENV:REDIRECT_STATUS} ^$
ReWriteCond %{HTTP_HOST} !maindomain.com
ReWriteCond %{HTTP_HOST} (.*)
ReWriteRule ^(.*)$ %1/$1 [L]

alternatively; 交替; to map the folder to the domainname without the ltd. 将文件夹映射到没有有限公司的域名。

RewriteCond %{ENV:REDIRECT_STATUS} ^$
ReWriteCond %{HTTP_HOST} !maindomain.com
ReWriteCond %{HTTP_HOST} ^([^\.]*)
ReWriteRule ^(.*)$ %1/$1 [L]

Not exactly what you wanted, but it might work for you, and doesn't need any php. 不完全是你想要的,但它可能适合你,并且不需要任何PHP。

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

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