
Intro
Drupal Clean URLs improve website usability and SEO by removing query strings (like ?q=) from URLs, making them more readable and user-friendly. Instead of www.test-drupal.com/?q=node/1
, Clean URLs allow www.test-drupal.com/node/1
.
This feature enhances navigation, improves search engine ranking, and provides a better user experience.
This is why those clean URLs must be properly generated and must look good, excluding any not needed technical data or text.
Problem
In some cases, there is a possibility that Drupal internal caching system can produce cached random URL's that have "/index.php/
" text added to a clean URLs just after domain name. The 'index.php
' URLs can cause problems with the facet search, trigger duplicated content warning in Google Search console and many more.
This issue can affect listed versions: Drupal 11, Drupal 10, Drupal 9, Drupal 8
Another topics of same issue:
hide index.php from Drupal clean url
index.php is inserted in menu links
index.php append on images src / sitemap / css - Cache Rebuild Resolves
URL sometimes includes index.php
Example: www.test-drupal/index.php/node/1
Solution
To fix this problem, we need to add a redirect rule to file .htaccess, that will redirect http request on web server level to a correct URL, by doing this, Drupal will correctly cache accessed URL, so dependent cache bin will have correct meta data.
RewriteRule ^index\.php/(.*) /$1 [R=301,L]
File: .htaccess inside Drupal project (/web/)
This url rewrite code need to be added to .htaccess file, in an empty line around line 98

Important
After implementing this fix, required to drop or rebuild Drupal cache (All Cache)
Conclusion
Having correct Clean URLs in Drupal is a simple and yet powerful way to enhance your website’s usability. If you encounter issues similar to one shown in this tutorial now you can use rewrite rules to resolve them.