Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Htaccess: How to get Variable form URL?

I do have a question regarding htaccess. I am trying to do the following:

blabla.com/lala and blabla.com/lulu

lala and lulu are my variables (showing different content on both sites, but it is the same file (example.php).

So what I did in htaccess is:

RewriteRule ^([^&]+)$ example.php?type=$1

But when echoing GET[type] I just get the filename("example.php"). But I want it to show "lala" or "lulu".

It works when I do it like that:

RewriteRule ^blablabla/([^&]+)$ example.php?type=$1

But I just don't need "blablabla" :D

Thanks in advance :)

like image 306
user1774597 Avatar asked Dec 15 '25 17:12

user1774597


1 Answers

But when echoing GET[type] I just get the filename("example.php"). But I want it to show "lala" or "lulu".

This is because rewrite rules loop until the URI going into the rewrite engine is the same one that comes out of the engine. Your regex ^([^&]+)$ is matching the target of your rule (example.php). So you either need conditions to check if the URI isn't already example.php or you check if the request points to a file or directory that doesn't exist:

RerwiteCond %{REQUEST_URI} !/example\.php
RewriteRule ^(.+)$ example.php?type=$1 [L,QSA]

or

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ example.php?type=$1 [L,QSA]
like image 190
Jon Lin Avatar answered Dec 17 '25 06:12

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!