如何在htaccess文件中使用自定义php文件中的变量?



我使用Apache 2.4和PHP 7.4结合WordPress。在服务器上,我托管MP4格式的视频,这种格式在互联网上不应该是免费的。所以我修改了htaccess文件,只允许某些网站访问和检索这些内容。缺点是这些文件不能再下载,也不能再流式传输到Chromecast上。我想在某些情况下允许这样做。也就是说,我想实现这是可能的,如果你有相同的ip地址作为服务器。服务器是在我的家庭网络,我希望家庭网络的所有用户能够自由访问它。其他人都没有。这是我的Apache文件当前的样子:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_URI} /wp-content/uploads/ [NC]
RewriteCond %{HTTP_REFERER} !^https://tv.xn--dnte-0ra.de/ 
RewriteCond %{HTTP_REFERER} !^https://fileserver02.syn-nas.provided-by-versatel.duentetech.de/
RewriteCond %{HTTP_REFERER} !^https://mediathek.xn--dnte-0ra.de/ 
RewriteCond %{REMOTE_ADDR} !^92.117.246.86 //That was the server's (and network's) IP address a few days ago. As long as the IP address does not change, this type of access works. Due to the dynamic IP addresses, this address changes almost daily. Therefore, this should be entered automatically when the page is called up.
#RewriteCond %{REMOTE_ADDR} !^$server_ip;
RewriteRule .mp4 / [R=301]
</IfModule>
#Mediathek-Zugriff
# BEGIN WordPress
# Die Anweisungen (Zeilen) zwischen „BEGIN WordPress“ und „END WordPress“ sind
# dynamisch generiert und sollten nur über WordPress-Filter geändert werden.
# Alle Änderungen an den Anweisungen zwischen diesen Markierungen werden überschrieben.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
如您所见,我使用了一个名为"$server_ip"的变量。该变量构建在一个php文件中,该文件与htaccess文件位于同一目录下。

这是php文件:

<?php
$ip_address = file_get_contents('http://checkip.dyndns.org');

$ip_address = preg_replace('/[^0-9.]/', '', $ip_address);

$server_ip = $ip_address;

$file = fopen("ipneu.txt", "a");

fwrite($file, $server_ip . "n");

fclose($file);
#putenv("IPADRESSE=$server_ip");
?>

最后,我想确保PHP文件输出的IP地址在RewriteRule中。

你觉得怎么样?

抱歉可能愚蠢的问题,但不幸的是,我不是很熟悉apache服务器配置autoin。

非常感谢!

的问候卢卡

我有几个解决办法,但总是无果而终。

  1. 将IP地址传输到
  2. 变量下的htaccess文件中
  3. IP地址被写入同一目录下的一个文件,并从那里传递到htaccess文件。最后,我想确保PHP文件输出的IP地址在RewriteRule中。

根据我的尝试,我得到了http 500失败,或者没有效果。我尝试过的一件事是:Php_value auto_prepend_file/pfad/zur/datei.php

你不能在htaccess文件中运行php文件。htaccess是apache的一个配置文件,它运行在其他文件之前。

您现在依赖于外部服务(dyndns.org),这不是最好的选择。你不做任何检查,如果API工作或不。

您可以使用$_SERVER['SERVER_ADDR']来确定服务器的IP地址。

你可以检查HTTP_HOST在htaccess检索服务器的主机,但你需要检查它是否是一个IP地址。

另一个建议:

如果你想从你的家庭网络自由访问它真的很容易:

设置一个域(有一些免费的服务)并为服务器的本地IP地址(192.168.0.something)设置a记录。名字也可能毫无意义,因为只有你自己知道那是什么。

最新更新