DEV Community

Cover image for Why You Should Avoid Dynamic URLs
Antonello Zanini for Writech

Posted on • Originally published at writech.run

Why You Should Avoid Dynamic URLs

SEO-friendly URLs meet the needs of searchers and users. In a world ruled by social media, the easier a link is to remember and share, the better. An ideal URL should be short, easy to read, and easy to understand. This helps in securing a company brand. This article will look into why you should avoid dynamic URLs and how you can replace them with static-looking URLs.

Two Types of URLs

URLs can be classified into two classes: static and dynamic.

Static URLs are not dynamically generated and do not accept string parameters. This means that the only way to modify the content of such a web page is by changing the HTML code.

Examples:

  • https://www.mydomain.com/articles/my-first-article

  • https://www.mydomain.com/en/articles/1/my-first-article

Dynamic URLs contain a query string, which is a part of the URL that assigns values to specified parameters. These are passed directly to the server and used to dynamically retrieve content from a database. The page will display the page accordingly, which will change based on the result of the query, without the need to touch the HTML code. In this case, such a web page acts just like a template for the content.

Examples:

  • https://www.mydomain.com/article.php?id=1

  • https://www.mydomain.com/articles/page.php?article_id=1&lang=en

Static vs. Dynamic URLs for SEO

Static URLs tend to obtain a better ranking in the search engine result pages. This is based on the assumption that search engines find it difficult to crawl and analyze dynamic URLs, which include parameters in them.

Plus, static URLs perform better in terms of clickthrough rate. Users can remember, understand, and read them easily and if a static URL matches the description and/or title of a website, they will likely click on it.

Another important aspect is that static URLs are easier to communicate by voice and share than dynamic URLs.

There are many other good reasons why static URLs are better for SEO than dynamic URLs. Let's examine a few of them:

  • From a certain point of view, dynamic URLs are nonexistent for search engines, because they index the web by crawling it, not by filling in forms

  • Dynamic URLs can consist of multiple parameters and if there are different indexed versions, these can quickly lead to duplicate content, which is not good in terms of SEO

  • Dynamic URLs are dirty and confusing, which results in poor clickthrough rate in search result pages

  • Dynamic URLs are often not described by keywords, which is not good from a semantic point of view

  • URL parameters make dynamic URLs longer than static URLs, and longer URLs are usually indexed more slowly than shorter ones.

Turning Dynamic URLs Into Static-Looking URLs

If your website is served by an Apache webserver, this can be easily achieved by adding some specific rewrite rules to your .htaccess file.

The parameters inside your .php files will be retrieved as usual. What you can change are all the dynamic links inside your code, which you should replace with custom-generated static-looking URLs.

First of all, make sure you already have a .htaccess file in the root folder of your website, otherwise create a new one.

Example 1

Let's say you want to turn https://www.mydomain.com/articles.php?id=1 into https://www.mydomain.com/articles/my-first-article.

To do so, add to your .htacess these lines:

[`Options +FollowSymLinks`](https://httpd.apache.org/docs/2.4/mod/core.html#options)
Enter fullscreen mode Exit fullscreen mode

Example 2

Now your goal is to convert https://www.mydomain.com/articles/page.php?article_id=1&lang=en into https://www.mydomain.com/en/articles/1/my-first-article.

To achieve such a result, you can put into your .htacess the following lines:

[`Options +FollowSymLinks`](https://httpd.apache.org/docs/2.4/mod/core.html#options)
Enter fullscreen mode Exit fullscreen mode

Please note that now the article.php page will be loaded without any JS, CSS, or images retrieved with relative URLs. This happens because they will be searched under the $1 folder, which doesn't exist. As described here, you can fix this by adding a element in the <head> section of your HTML document as follows:

<base href="/articles/">
Enter fullscreen mode Exit fullscreen mode

Put the path to the folder where the original .php file is placed inside the href attribute. This will be used as the base URL for all relative URLs on the page.

Bonus

Finding the working rewrite rule might be a complex task, especially for beginners. This is why I recommend the following two tools:

Conclusion

Static URLs are more SEO-friendly and shareable than dynamic URLs. For this reason, you should always avoid using the latter. Converting a static URL into a dynamic URL is easy, as I have just shown. By adding some specific rewrite rules, you can replace all your dynamic URLs with more human-readable static URLs.

Thanks for reading! I hope that you found this article helpful.


The post "Why You Should Avoid Dynamic URLs" appeared first on Writech.

Top comments (0)