Mod_rewrite is an Apache’s module that provides URL manipulation capability. It makes the URL’s look cleaner and friendly on websites.
I learned about it when I was working on my CMS project. Here is how I did it:
Step 1. Check if mod_rewrite is enabled:
Look at your Apache’s 「httpd.conf」 file. If you are using a XAMMP like me it is located in the etc folder. If you see #(commented out) before it, take off and reload your apache.
Step 2. Create a .htaccess file:
Every mod_rewrite rules must be with the commands ‘RewriteEngine on’. So you need to type this at the top of the file.
Next, we are going to rewrite a rule that redirects to a ‘registration.php’ page if we request the URL http://cms/registration :
In the above rule, ‘registration’ is the pattern that should be matched and redirected to ‘registration.php’. The command ‘[NC]’ means that the rule is case insensitive. ‘^’ indicates that it is matching any text after the IP address or domain name while ‘$’ signifies the end of the URL.
Step 3. Edit the URL:
Example: registration.php → registration
That’s it. You did it 🙆🏻♀️