使用 PHP 编辑 .htaccess

编程入门 行业动态 更新时间:2024-10-17 15:34:15
本文介绍了使用 PHP 编辑 .htaccess的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

    本文发布于:2023-11-01 12:59:13,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1549399.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:编辑   PHP   htaccess

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!