SEO Friendly URL in PHP using htaccess

SEO is a number-one priority for most website owners nowadays. If you want to know how to create an SEO-friendly URL, then read on.

A URL (uniform resource locator) is simply the address of any document or file on the web. It’s basically like someone giving you directions to their house, but instead of saying “turn left onto Main Street at the Taco Bell”, they say “/house/address2/” (where “/address2/” was your friend’s home). You can learn more about what makes up a URL by taking this free online tutorial.

How to create an SEO-friendly URL in PHP using htaccess?

It may be possible that you want to redirect a page or show a specific template based on GET method.

For example, if you want to redirect the URL to the following URL You can implement this as follows −

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ \index.php?page=$1 [QSA,L]
  • RewriteCond %{REQUEST_FILENAME} !-f this condition will check if it is not a file.
  • RewriteCond %{REQUEST_FILENAME} !-d this condition will check if it is not a directory.
  • ^(.*)$ it will match the url and pass the $_GET value to index.php.
  • In [QSA,L], QSA will append query string and L will stop precessing the .htaccess file.

Now depending on GET request, you can dynamically redirect to a specific page template.


switch ($_GET['page']){
	case "home":
		echo "Welcome to home page";
	case "about-us":
		echo "Welcome to about us  page";
	case "contact":
		echo "Welcome to contact us  page";
		echo "Welcome to demo page";

So this is the basic example of how to generate a user-friendly pretty URL using htaccess.

How to create php seo friendly URL without htaccess?

Are you creating your website, but afraid of Put htaccess file because it’s hard to write htaccess file syntax? If yes, I have a solution for this. I will tell you how to create PHP SEO-friendly URL without htaccess . So let’s start…

You have to use index.php in your URL. Without a .htaccess file, you can’t remove index.php from the URL

var_dump( parse_url(SELF_URL_PATH) );

The output will be

array(1) { ["path"]=> string(27) "" }

Now you can easily extract the fragments from the parse_url() function.

About Ashis Biswas

A web developer who has a love for creativity and enjoys experimenting with the various techniques in both web designing and web development. If you would like to be kept up to date with his post, you can follow him.

Leave a Comment