I wondering how I should code an index.php/controller to be able to display an image using an url like domain.com/imageid. Right now I'm displaying images using e.g domain.com/?image=imageid and collect it with $_GET superglobal array.
1 Answer
Just use rewrite_mod of apache.
In your root directory, create a file named .htaccess and write this in it :
RewriteEngine On
RewriteRule ^(.+)$ index.php?image=$1
3 Comments
user1683645
Okay thanks. Looks like some sort of regex? Does this rewrite domain.com/?id=imageid to domain.com/imageid only? Also I'm using ubuntu and apache2. I'm assuming by root directory its the apache2 directory I should create htaccess in? Going to read up more about it.
Thomas Ruiz
When i'm saying root directory, i'm talking about the root of your website (/var/www by default), not of your server. And yes, it does only this redirection. It is using regex, so you can write every rules you want.
user1683645
I failing to apply this rule on my website. I have an url e.g domain/?i=345 where 345 is the image id. I've created the .htaccess file in the website root directory and applied the rules you said but the url string still shows domain/?i=345