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

问题描述

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

  RewriteEngine On 
RewriteBase /

#重定向映射域
ReWriteCond%{HTTP_HOST} joshblease.uk.to
ReWriteCond%{REQUEST_URI}!gme-index /
ReWriteRule ^(。*)$ gme -index / $ 1 [L]

有没有办法编辑/添加额外的域地图到文件使用PHP?



只需要获取.htaccess文件的内容,并使用PHP脚本添加到他们。

解决方案

如上述其中一条建议,最好使用,而不是直接从PHP代码编辑.htaccess。以下是一个示例如何使用它:


  1. 在httpd.conf文件中添加以下行:

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


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

      sub1 / subdir1 
    sub2 / foodir2
    foo / bar
    pre>

  3. 将这些行添加到.htaccess文件中:

     选项+ 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



优势:您可以从PHP代码编辑或重新创建文件 /path/to/domain-dir.txt ,而不会打开一个巨大的安全漏洞来允许php代码o直接编辑.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]

Is there any way to edit/add extra domain maps to the file using PHP?

Simply, I want to get the contents of the .htaccess file and add to them using a PHP script.

解决方案

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. Add following line to your httpd.conf file:

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

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

    sub1 /subdir1
    sub2 /foodir2
    foo /bar
    

  3. 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

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 09:08