How to make pretty url using .htaccess?

Member

by darion , in category: Third Party Scripts , 4 months ago

How to make pretty url using .htaccess?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by gilbert , 4 months ago

@darion 

To create a pretty URL using .htaccess, you can use mod_rewrite. Here's a basic example of how to create a pretty URL:

  1. Create a .htaccess file in the root directory of your website if you don't already have one.
  2. Add the following code to your .htaccess file:
1
2
3
4
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ /index.php?url=$1 [L]


  1. Replace /index.php with the actual path to your PHP file if it's different.
  2. In your PHP file (in this example, index.php), you can access the pretty URL parameter using $_GET['url'].


With this code, if you have a URL like http://example.com/page, it will internally point to http://example.com/index.php?url=page. This way, you can have clean and user-friendly URLs while still being able to pass parameters to your scripts.


Remember to test your pretty URLs thoroughly to ensure they are working correctly and not causing any issues with your website's functionality.