I making a project for my a level where you will be able to search up a car by registration plate and such. So how do I make it work so that say i have example.com/vehicle/registration. So that it always loads index.php and passes the registration through so that it can be looked up in a database. I've tried searching this up but im not sure exactly how to word it to find the correct answer.
I've tried changing .htaccess as im assuming that's probably how you do it but haven't had any success. This is probably a really easy thing to do but some pointers in the right direction would be much appreciated
You are quite right in that you're looking to use .htaccess
for this. I actually cannot find a simple SO post about this to direct you to, so I'll try explain briefly.
There are a few steps involved before you can use .htaccess
. Firstly, it needs to be enabled. This will depend hugely on your platform, and permissions. Some hosts may not even allow you to use htaccess files at all.
AllowOverride
on the directory which will contain your .htaccess file.<Directory /var/www/public_html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
Ensure that mod_rewrite
is installed and enabled as a module for your HTTP server. How to achieve this depends on which server platform you're running.
Once done, you need to restart your HTTP server.
Create a .htaccess file with the following code inside:
<IfModule mod_rewrite.c>
# Turn on Rewrite Engine
RewriteEngine On
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
# If the request is not for a valid link
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^vehicle\/(.*?)\/?$ index.php?reg=$1 [QSA,NC,L]
</IfModule>
This will assume that your .htaccess
file and index.php
files are in the parent directory, and there is no need for a vehicle
folder. To be precise you'll have
/var/www/public_html/.htaccess
and /var/www/public_html/index.php
I hope this helps somewhat. A good reference for checking .htaccess
files is https://htaccess.madewithlove.com and obviously a regex checker like https://regex101.com!