How to Create SEO Friendly URL with Dynamic Content Using PHP

Creating an SEO-friendly URL has become a necessity for businesses in present times. An SEO-friendly URL not only makes it easier for users to discover your website with a logical hierarchy, but also makes URLs readable by both the machine and users. Using such URLs ensures easier discoverability for your business and find out what they need from your web pages with relative ease.

There are multiple thumb rules that shall be adhered to in order to make an SEO-friendly URL, which make the URLmore legible and easily discoverable. Here are the 10 basic guidelines:

  1. URLs must be similar to page titles
  2. Avoid having too many folders
  3. Separate words using hyphens
  4. Avoid using special characters in URLs
  5. Plain words for page names, folders, and paths
  6. Try using lowercase URLs only
  7. Single domains are better
  8. Use subdomains only when necessary
  9. Use keywords within the URL
  10. URLs should be similar to page titles

For example, this URL is NOT SEO-friendly.

https://cdn03.example.com/8cb42/index.php?35872=8zh3n9vadbxgac9c&id=851368#top
This URL is SEO-friendly:


https://www.softwaresuggest.com/blog/category/review

Using .htaccess to Improve URLs

There are a number of ways in which you can improve your URL using .htaccess, which are enumerated below:

Remove File Extensions: Many websites have file extensions that produce unnecessary clutter within the URL. Though they are fine if we see from a technical SEO perspective, an SEO friendly URL in PHP can improve the user experience.

For instance, you can change the URL ‘xyz.com/cool-page.html’ to ‘xyz.com/cool-page’ or ‘xyz.com/cool-page.php’ by changing instances of ‘.html‘ for a cleaner user experience.
Redirect index.php to root: Most PHP-based content management systems contain an ‘index.php’ file, which affects the user experience and also looks bad. To fix this, you can try the following to redirect the‘index.php’ file to the root domain:


.htaccess

RewriteRule ^index.php$ https://www.example.com/ [R=301,L]

If this creates an endless redirect loop in your server, you can try using the method below:

.htaccess

RewriteCond %{THE_REQUEST} ^[A-Z][3,9]\/([^/]+/)*index\.php\ HTTP/
RewriteRule ^(([^/]+/)*)index.php$ https://www.example.com/$1 [R=301,L]

Using lowercase URLs

While responding to a question related to SEO URLs, Google’s John Mueller said that Google doesn’t care about the case that you use. However, URLs themselves are case sensitive. Therefore the URL ‘softwaresuggest.com/blog/‘ isn’t the same as ‘softwaresuggest.com/BLOG/‘. This often leads to duplicate content issues.

Additionally, some Linux servers have problems handling uppercase characters in URLs and often return a ‘502 – Bad Gateway’ header in the response. You can ensure that everything in your server and website is kept in lowercase characters by simply using a canonical tag. This is to check whether there are any uppercase characters present and replaces them with lowercase letters. Alternatively, you can use ‘mod_speling’ to correct the URL case if WebDAV is enabled in any of your subdirectories. 

Also, if you can access the ‘httpd.conf‘ file or your host provider can make the changes for you, you can force lowercase URLs using .htaccess. Using a mod_rewrite directive as a RewriteMap, which allows predefined substitution strings to be used in a RewriteRule. In most webmasters, the RewriteMap can’t be used in <Directory> sections or even .htaccess. However, it can be changed in the server context using the https.conf file. 

Warning: 

Many contemporary image tools save images with file extension names in uppercase, e.g. .JPG. This causes images to fail to work in certain instances. Thus, it is wise to check if the file extensions are in lowercase before forcing lowercase URLs on your website.

Rewriting Dynamic URLs

Dynamic URLs have no correspondence to the physical file structure of the server. Hence, they might not always be very readable. In such cases, we can improve the URL’s look by using the .htaccess file for some beautification. Nobody would like a URL to read as ‘example.com/blog.php?id=129‘, and we can fix it easily using the following code:


.htaccess

RewriteRule ^epic-blog-post$ blog.php?id=epic-blog-post [L]
RewriteCond %{THE_REQUEST} ^[A-Z][3,9] /blog.php?id=129 HTTP/
RewriteRule ^blog.php$ https://www.example.com/epic-blog-post? [R=301,L]

The above code will change the URL to ‘example.com/epic-blog-post’, which looks much better.

Conclusion: .htaccess is not the ultimate fix

It must be mentioned here that if your URL is not SEO friendly at all, .htaccess won’t magically fix everything. Creating redirect rules that rewrite URLs logically with a single redirect command is difficult. It is better to use server-side languages like Python or PHP to develop SEO-friendly URLs in the initial phase. Hence, it is better to create everything from scratch rather than being partially successful.Lastly, in some instances, you need to use a combination of code and .htaccess to get the result you need. But using .htaccess to fix all URL issues isn’t advisable.

Leave a Reply

Your email address will not be published. Required fields are marked *