I have the following .htacess file:
RewriteEngine On
RewriteBase /
# Remove 'www' from the URL
RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# Redirect requests to index.html if the request is not for an existing file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.html [L]
My goal is the follwing:
- https://www.example.com/a/anID should be redirected to
https://example.com/index.html - https://example.com/a/anID should be redirected to
https://example.com/index.html - https://www.example.com/index.html should be redirected to
https://example.com/index.html - https://example.com/index.html should be kept as
https://example.com/index.html
These is because I have an Angular application that will handle these scenarios if the URL doesn’t point to an existing file or directory.
I’ve tried multiple variations of my .htaccess through this website and I’m unable to achieve these four rules.
Thanks in advance!
4
The final solution is:
RewriteEngine On
RewriteBase /
# Remove www and redirect to the non-www version
RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]
RewriteRule ^(.*)$ https://YourBaseHost/$1 [R=301,L]
# Ensure this block is processed only if not already redirected
RewriteCond %{REQUEST_URI} !^/index.html$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.html [L]
Being your “YourBaseHost” the base host you want to redirect to.
With this it will work OK.
My issue was that “%1” was not getting properly the base host.