0

I want to achieve the following behavior with the least number of rules:

  1. Enforce HTTPS
  2. Redirect www to non-www
  3. Remove .php and .html extensions from all URLs
  4. Redirect example.com/index.php and example.com/index to example.com
  5. Redirect example.com/static/*.html to example.com/*
  6. Remove trailing slashes

Right now I use the following rules:

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

RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [R=301,L]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*?)/?$ /$1.html

RewriteCond %{THE_REQUEST} ^.*/index
RewriteRule ^(.*)index /$1 [R=301,L]

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

which don't include a rule for the static directory as I don't have introduced it yet. I plan to do it for caching reasons now.

12
  • And what is your question?
    – arkascha
    Dec 4, 2021 at 20:35
  • What the title says. If the same results can be achieved with less rules.
    – Costas
    Dec 4, 2021 at 20:47
  • I doubt you can shorten this... But even if that were possible - why? Less rules does not necessarily mean things are more effective. And certainly things are harder to maintain.
    – arkascha
    Dec 4, 2021 at 22:55
  • I see an issue with your implementation though: as I see it a request to http://example.com/some/path would not get redirected to https, right?
    – arkascha
    Dec 4, 2021 at 22:56
  • Everything redirects to https. If my implementation has a mistake then another setting of the server makes the redirection. If the server has to process less conditions and make less rewrites why things wouldn't be more effective?
    – Costas
    Dec 5, 2021 at 0:23

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.