Subject: .htaccess not working correctly—causes infinite loading or incorrect URLs
Hello,
I am experiencing issues with my .htaccess file. The file does not work correctly on my hosting, but when I test it on my local Apache server, everything works fine.
Here is the .htaccess file I am using:
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^nl$ index.html [NC,L]
RewriteRule ^en$ index_eng.html [NC,L]
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType text/css "access plus 1 year"
ExpiresByType text/javascript "access plus 1 year"
ExpiresByType application/javascript "access plus 1 year"
</IfModule>
The problem is inconsistent across different browsers:
In some browsers: The page keeps loading indefinitely, but the URL in the address bar is correct (e.g., https://damirelzuhan.com/en).
In other browsers: The page loads correctly, but the URL does not change as expected (it remains as index_eng.html).
Additionally, on mobile devices, I sometimes get a 404 error when trying to access /en or /nl.
I suspect that the issue might be related to the Options +FollowSymlinks directive or some hosting restrictions. Could you confirm whether my .htaccess rules are supported on your server? If not, how should I modify them to work correctly?
I can’t recreate the symptoms your having. But that said, I tweeked your .htaccess to ignore rewritten url’s to stop the potential infiniate loading loop.
Options +FollowSymlinks
RewriteEngine On
# Prevent looping by excluding rewritten URLs
RewriteCond %{REQUEST_URI} !^/index\.html$
RewriteCond %{REQUEST_URI} !^/index_eng\.html$
RewriteRule ^nl$ index.html [NC,L]
RewriteCond %{REQUEST_URI} !^/index\.html$
RewriteCond %{REQUEST_URI} !^/index_eng\.html$
RewriteRule ^en$ index_eng.html [NC,L]
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType text/css "access plus 1 year"
ExpiresByType text/javascript "access plus 1 year"
ExpiresByType application/javascript "access plus 1 year"
</IfModule>
Thank you for your suggestion! However, the problem still persists.
In some browsers, after switching the language, the URL initially shows https://damirelzuhan.com/en, but after the page loads, it changes to https://damirelzuhan.com/index_eng.html.
In other browsers, the page loads correctly, but the URL never changes to /en.
On mobile devices, I sometimes get a 404 error when trying to access /en or /nl.
It seems like the browser is still seeing the actual file path (index_eng.html) instead of keeping the rewritten URL (/en).
Could there be an issue with how the server handles rewrites? Or do I need to use a different approach to force the browser to stay on /en?
Any further suggestions would be greatly appreciated!
I checked your website, and there are a couple of different mechanisms you have setup to do the URL rewriting:
There is the .htaccess RewriteRule to rewrite /en to /index_eng.html.
There is an additional Redirect at the bottom of the file to redirect /index_eng.html back to /en.
On the page itself is a script rewrite.js, that will also redirect /en to /index_eng.html.
Some of these rewrites/redirects are clashing which is causing the redirect loop issues.
For starters, by far the easiest solution to fix this is to just create a directory with the name en and put the index_eng.html in it, renaming it to index.html. Then you can just get rid of all the redirects and rewrites and have the URLs work exactly as you want them to.
If you want to keep the current file structure, you can probably do it. But then I would suggest you stick to RewriteRule (and possibly RewriteCond) directives only. Mixing in Redirect directives and Javascript redirects adds more moving parts which can cause issues.