是否可以在 htaccess 文件中创建或调用变量

问题描述 投票:0回答:1

我问这个问题的原因是因为......在

.htaccess
文件中我想使用
RewriteRule
将我的
index.php
更改为一些随机字符串即如果我从浏览器请求
www.example.com/index.php
,我想要在浏览器中显示的 URL:
www.example.com/abcdefghij1234
。我希望
abcdefghij1234
是动态的并且随机变化。 P.S 我根本不想
index.php
出现在URL中,只是随机生成的字符串。

这是我在

.htaccess
文件中设置的规则,但它没有用。请帮助我

RewriteEngine On

RewriteBase /

RewriteCond %{ENV:REDIRECT_STATUS} ^$

RewriteRule ^generate_random_string$ - [E=RANDOM_STRING:%{TIME}%{REMOTE_ADDR}]

RewriteRule ^generate_random_string$ - [L]

RewriteRule ^%{ENV:RANDOM_STRING}$ index.php [L]

RewriteCond %{THE_REQUEST} ^GET\ /index\.php [NC]

RewriteRule ^index\.php$ /%{ENV:RANDOM_STRING} [L,R=301]

谢谢支持

regex .htaccess url mod-rewrite
1个回答
0
投票

感谢您的回复。抱歉这么晚才回复,我正忙于学校项目和其他任务。回到我的问题。

实际上,我正在玩 PHP,我编写了以下代码并将其上传到我的 cpanel。

$domain = $_SERVER['HTTP_HOST'];
$path = "/index.php";
$destina_url = "https://{$domain}{$path}";
header("Location: $destina_url");
exit();

我把上面的代码保存为public文件夹下的code.php。我被重定向到www.mydomain.com/index.php。此外,我继续在 htaccess 中使用以下代码重写 url

  RewriteEngine On
  RewriteBase /

  RewriteRule ^abcdefghijke12345$ index.php [L]
  RewriteCond %{THE_REQUEST} ^GET\ /index\.php [NC]
  RewriteRule ^index\.php$ abcdefghijke12345 [L,R=301]

所以,当我在浏览器中启动我的完整网址时,即(www.mydomain.com/code.php),我被重定向到(www.mydomain.com/abcdefghijke12345),这是期望的结果。

但是,我手动将 abcdefghijke12345 输入到 .htaccess 中,这对我来说是静态的,我想要的是 abcdefghijke12345 来自动生成随机字符串。

所以,我在 code.php 中添加了以下代码:

$domain = $_SERVER['HTTP_HOST'];

$path = bin2hex(random_bytes(128));

$destina_url = "https://{$subdomain}.{$domain}/{$path}";
header("Location: $destina_url");
exit();

使用此代码,当我再次在浏览器中启动 (www.mydomain.com/code.php) 时,浏览器出现错误 403。所以,我很好奇是否可以在我的 code.php 中创建一个服务器变量并在 .htacess 中引用它,如果我问得对的话我不会这样做。请原谅我的英语。

谢谢。

© www.soinside.com 2019 - 2024. All rights reserved.