编辑的.htaccess用PHP [英] Edit .htaccess with PHP

查看:229
本文介绍了编辑的.htaccess用PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个域映射到一个文件夹中的.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.

任何帮助AP preciated,约什。

Any help appreciated, Josh.

推荐答案

至于建议中的上述意见,最好是使用的 RewriteMap指令你这里的情况,而不是试图编辑从PHP $ C $的.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:

  1. 添加下面一行到你的httpd.conf文件:

  1. Add following line to your httpd.conf file:

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

  • 创建一个文本文件作为 /path/to/domain-dir.txt 是这样的:

  • Create a text file as /path/to/domain-dir.txt like this:

    sub1 /subdir1
    sub2 /foodir2
    foo /bar
    

  • 添加这些线在你的.htaccess文件:

  • Add these line in your .htaccess file:

    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
    • 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 code,而无需打开一个巨大的安全漏洞进行的.htaccess直接让PHP $ C $共同编辑。

    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.

    这篇关于编辑的.htaccess用PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆