Many websites are still using long and confusing URLs that look something like this:

example.com/service-digital-marketing.php?id=1230#.

They usually have random letters and numbers that don’t make any sense. These URLs are just long and random, but they affect the user experience and can also hurt your website’s visibility and ranking in search engines.

If your website runs on an Apache server, there’s a unique solution for this problem. By using the .htaccess file with mod_rewrite , you can change those messy URLs into clean and SEO-friendly links that can be easily readable by both users and crawler bots.

By the end of this article, you will know the exact steps to change your dynamic URLs into readable and SEO-optimized ones that both users and search engines will love.

Why SEO-Friendly URLs Matter

Before trying to change your URLs, you need to understand why changing the existing URLs in your website matters.

An SEO-friendly URL is easy to read and gives clear information about what is on the page of your website or your blog. For example:

example.com/product.php?shoe=3&item=11
example.com/shoes/sneakers

Benefits of SEO-friendly URLs For Your Website:

The first step is to ensure that the WordPress permalink settings are configured correctly:

  • Better user experience: Users are more likely to open URLs or links that can be easily understood just by looking at the URL.
  • Increase in SERP rankings: Search engine bots read the URL and any keywords in it to understand what content is on the page, so a clear URL often gets higher rankings.
  • High click-through rates: URLs that look better on the search results page get more clicks and can be used in social media or other platforms for more click-throughs.

What Does .htaccess and mod_rewrite Mean In A Website?

For websites that run on Apache servers, the .htaccess file is very important because it allows the website owners to change or control how their website performs certain actions, redirects, error pages, access controls, and URL rewriting as well.

mod_rewrite in Apache lets you change (rewrite) how the URLs in your website appear in a user’s browser without you having to change the entire file structure.

Both help you in changing the long, random URLs into clean links.

Requirements for Changing the URLs in Your Website

  • Enable mod_rewrite

    In your website’s Apache server, make sure that mod_rewrite is enabled. After it is enabled, restart Apache.
  • Allow .htaccess Overrides

    On your Apache configuration file, allow .htaccess overrides using:
        <Directory "/var/www/html">
    AllowOverride All
    </Directory>
    

How to Rewrite Website URLs Using .htaccess

Once you have enabled mod_rewrite and .htaccess, you can start editing your .htaccess file in the website’s root folder.

We will go through some examples of how you can rewrite some URLs

1. Rewrite Engine Should be On

Make sure that the RewriteEngine is on before changing the URLs.

2. To Add Keywords In The URLs

Use this code:

                                    
RewriteRule ^blog/([a-zA-Z0-9-]+)/?$ blog.php?post=$1 [L]
                                         

To change: blog.php?post=my-first-blog

Into: example.com/blog/my-first-blog

3. To Remove File Extensions

Use this code:

                                    
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)$ $1.php [L]
                                         

To change: example.com/about-us.php

Into: example.com/about-us

4. Remove or Add Trailing Slashes

Use this code to remove the trailing slashes:

                                    
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]
                                         

Use this code for adding the trailing slashes:

                                    
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ https://www.example.com/$1/ [R=301,L]
                                         

5. To Remove Query Strings

Use this code:

                                    
RewriteEngine On
RewriteRule ^product/([0-9]+)/?$ product.php?id=$1 [L, QSA]
                                         

To change: example.com/service.php?id=13

Into: example.com/service/13

Examples and Best Practices for SEO-Friendly URLs

Examples of URLs

Good for SEO: /blog/digital-marketing-services

Bad for SEO: /blog/Digital_Services_2025?id=76&track=xyz

  • Add keywords naturally
  • URLs are case-sensitive, which means, using all lowercase letters is considered good for SEO
  • Your URLs should be short and easily understood by bots and users too.
  • Special characters like @, %, ! can affect your SEO-friendly URLs.
  • Use - (hyphens) for two or more words in the URL instead of separating them with underscores _
  • Avoid adding IDs, strings, or unnecessary parameters in the URLs

Rewriting URLs in Other Platforms

If you’re using a CMS like WordPress, URL rewriting will be automatically available because of the built-in settings or plugins.

Using .htaccess gives you more control in changing the URLs if your site is built from scratch or uses custom PHP files.

For Laravel and CodeIgniter, URL rewriting is in their routing systems, but it works in a different way.

Key Takeaways

SEO-friendly URLs are a powerful improvement for your website URLs. They make your links easier to read, share, and rank on search engines. Whether you want to clean up query strings, remove file extensions, or make your product page more clickable, you can use these codes to start rewriting your URLs.