我有一个将域映射到文件夹的 .htaccess.
I have a .htaccess that maps a domain to a folder.
RewriteEngine On RewriteBase / # redirect mapped domain ReWriteCond %{HTTP_HOST} joshblease.uk.to ReWriteCond %{REQUEST_URI} !gme-index/ ReWriteRule ^(.*)$ gme-index/$1 [L]有没有办法使用 PHP 编辑/添加额外的域映射到文件中?
Is there any way to edit/add extra domain maps to the file using PHP?
简单地说,我想获取 .htaccess 文件的内容并使用 PHP 脚本添加到它们中.
Simply, I want to get the contents of the .htaccess file and add to them using a PHP script.
推荐答案正如上述评论之一所建议的,最好使用 RewriteMap 在这里,而不是尝试直接从 PHP 代码编辑 .htaccess.以下是如何使用它的示例:
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. Here is a sample how to use it:
将以下行添加到您的 httpd.conf 文件中:
Add following line to your httpd.conf file: RewriteMap domainMap txt://path/to/domain-dir.txt
创建一个文本文件作为 /path/to/domain-dir.txt 像这样:
sub1 /subdir1 sub2 /foodir2 foo /bar在您的 .htaccess 文件中添加这些行:
Add these line in your .htaccess file:
Options +FollowSymLinks -MultiViews RewriteEngine on RewriteCond %{HTTP_HOST} ^(.*).domain$ [NC] RewriteRule ^$ ${domainMap:%1} [L,R]实际上,所有这些都意味着要有这些重定向:
Effectively all this means is to have these redirects in place:
- sub1.domain/=>sub1.domain/subdir1
- sub2.domain/=>sub2.domain/foodir2
- foo.domain/=>foo.domain/bar
优点: 有了这个设置,你可以从你的 php 中编辑或重新创建文件 /path/to/domain-dir.txt无需打开巨大安全漏洞的代码允许 php 代码 o 直接编辑 .htaccess.
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.
更多推荐
使用 PHP 编辑 .htaccess
发布评论