Report this

What is the reason for this report?

A tip to optimize the order in the .htaccess file

Posted on June 23, 2025

I have created an online project mywebsite.com and I am still looking for a solution to optimize all the links using Rewrite links. I created a .htaccess file and the surprise is that some of the links work perfectly and some are redirected. The question is what is the order of the rules in .htaccess or what else would need to be completed?

Lines like: www.mywebsite.com/the-sound-of-life :: go perfectly with one of the rules

And links like: www.mywebsite.com/daily.motivational.quotes-smile.through.a.happy.life.73.life :: is directed to index.php?pages= OR www.mywebsite.com/citate.motivationale.zilnice-zambeste.spre.o.viata.fericita.73.inspiratie

The .htaccess file is written like this:

Options +FollowSymlinks
RewriteEngine On

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
#RewriteRule ^(.*)$ $1.php [L,QSA]
#RewriteRule .*$ /index.php [L]

RewriteCond %{HTTP_HOST} !^www\.[a-z-]+\.[a-z]{2,6} [NC]
RewriteCond %{HTTP_HOST} ([a-z-]+\.[a-z]{2,6})$ [NC] 
RewriteRule ^([^.$]*)$ index.php?pages=$1 [L,R=301] 
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?pages=$1 [L,NC]

RewriteCond %{THE_REQUEST} ^[A-Z]{0,9}\ /.*\.php\ HTTP/
RewriteCond %{HTTP_HOST} !^www\.[a-z-]+\.[a-z]{2,6} [NC]
RewriteRule ^daily.motivational.quotes-([A-Z.0-9]+).life$ quotedet.php?pid=$1 [L,NC]
RewriteRule ^citate.motivationale.zilnice-([A-Z.0-9]+).inspiratie$ quotedet.php?pid=$1 [L,NC] 

#RewriteRule ^(.*)$ index.php?pages=$1 [L,NC] 
RewriteRule ^([A-Z]*)$ index.php [L,NC] 

RewriteCond %{HTTP_HOST} ^mywebsite\.com$ [NC] 
RewriteRule ^(.*)$ https://mywebsite.com/$ [NC,L,R=301]  
========

With appreciation for the support given in optimizing links via .htaccess



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hey!

In .htaccess, order matters a lot, rules are applied top to bottom, and the first match will win (especially with [L] flags).

In your case, some general rules (like index.php?pages=$1) might be catching requests before your more specific quotedet.php rules get a chance.

Try moving the more specific daily.motivational.quotes-... and citate.motivationale.zilnice-... rules higher up, before the general ones.

That should help fix the unexpected redirects.

- Bobby

Heya, @bec1cd09097d477da725b161a4066f

You can enable rewrite log (if possible) or insert message headers to see which rule is matching. This can help you with debugging

Regards

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.