0

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.

  • It's not possible. https://stackoverflow.com/a/30399556/2844703 – James Apr 28 '23 at 23:08
  • 1
    @james The question you link to doesn't have anything to do with this question. What the OP is trying to do here is kind-of possible (although "why" is another matter, it does feel like an ["XY problem"](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)?!) – MrWhite Apr 29 '23 at 00:57
  • 1
    What is an actual use case here? What URL are you intending to link to internally? Is there any significance of this "random number" or is it simply "ignored"? Is this "random number" intended to persist for a particular user or literally "random" for _every_ request? (Your rules are expecting some persistence.) – MrWhite Apr 29 '23 at 01:05
  • You can use a `RewriteMap` to generate random strings (https://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewritemap), however I fail to understand what you are trying to achieve here. That only works if `index.php` is actually requested in the first place, so that "non randomized" URL is already known and requested by the client. If you _really_ want to "mask" the actual URL that is requested, then you need to alter the links you send out prior to the them being used by the client. Which should not be done on protocol level but in your application logic. – arkascha Apr 29 '23 at 06:12
  • @arkascha, since i still need more clarification. I posted my other question and code as an answer since this comment space wont be enough for me to express it better. Pls kindly look at it. Thanks – father father May 04 '23 at 15:15
  • @MrWhite kindly look at the reason why i asked the question . Sorry for not puting up more clarity in my previous question. my english is bad, please accept my apology. i still need help. Thanks – father father May 04 '23 at 15:17
  • @arkascha I have done that. Thanks – father father May 05 '23 at 09:10

0 Answers0