The reason why I asked the question is because... In the .htaccess
file I want to use the RewriteRule
to change my index.php
to some random string i.e if I request www.example.com/index.php
from the browser, I want the URL to display like this in the browser: www.example.com/abcdefghij1234
. I want abcdefghij1234
to be dynamic and change randomly. P.S I don't want index.php
to appear at all in the URL, just the randomly generated string only.
Here is the rule I set in the .htaccess
file yet, it didn't work. Please help me
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]
Thanks for the support.
Thank you for responses. Apologies for replying this late, i was caught in school projects and other task. Back to my question.
Actually, I was playing around with PHP and i wrote the following code and uploaded it to my cpanel.
$domain = $_SERVER['HTTP_HOST'];
$path = "/index.php";
$destina_url = "https://{$domain}{$path}";
header("Location: $destina_url");
exit();
I saved the above code as code.php in the public folder. i was redirected to www.mydomain.com/index.php. Also, i proceed to rewrite the url with the following code in the htaccess
RewriteEngine On
RewriteBase /
RewriteRule ^abcdefghijke12345$ index.php [L]
RewriteCond %{THE_REQUEST} ^GET\ /index\.php [NC]
RewriteRule ^index\.php$ abcdefghijke12345 [L,R=301]
So, when i launch my complete url in the browser i.e (www.mydomain.com/code.php), i was redirected to (www.mydomain.com/abcdefghijke12345) which is the desired result.
However, i manually typed abcdefghijke12345 into the .htaccess which is static for me and what i want is abcdefghijke12345 to automate the generation of the random string.
So, i added below code to code.php:
$domain = $_SERVER['HTTP_HOST'];
$path = bin2hex(random_bytes(128));
$destina_url = "https://{$subdomain}.{$domain}/{$path}";
header("Location: $destina_url");
exit();
With this code, when i lauched (www.mydomain.com/code.php) again in the browser, I got error 403 from the browser. So, i was curious if it is possible to create a server variable in my code.php and reference it in .htacess, i don't if I'm asking that rightly. Pardon me for my english.
Thanks.