Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess with php to load randomfile

Tags:

php

.htaccess

I'm using apache2 and latest php what I want to do is load a random URL php file on my domain.

Example: example.com/randfilename.php?var1=1&var2=2&var3=3

Then the randomfilename.php will load file.php with vars. Keep in mind randomfilename.php will not be that name is will be random with .php at the end.

I tried with RewriteEngine and it only works for me for example.com/randfilename but once I add .php it doesn't work.

What I have tried is

RewriteEngine On
RewriteRule ^([a-zA-Z\-]+)$ file.php?key=$1 [L]

Is there another away of doing this to achieve what I'm trying to do.

like image 494
Abby E Avatar asked Mar 03 '26 23:03

Abby E


1 Answers

The .php extension has a . in it, and your pattern is ^([a-zA-Z\-]+)$, which matches only letters and dashes, no periods. You can explicitly add the .php extension to the pattern and exclude file.php:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/file\.php$
RewriteRule ^([a-zA-Z\-]+)\.php$ file.php?key=$1 [L]
like image 76
Jon Lin Avatar answered Mar 05 '26 11:03

Jon Lin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!