Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ilex01's avatar

htaccess redirect from www to non-www and from http to https with request uri goes to index.php

Can someone give me the .htaccess file for this:

htaccess redirect from www to non-www and from http to https with request uri goes to index.php

Currently, I've this error on the browser even when I clear my data: ERR_TOO_MANY_REDIRECTS

0 likes
2 replies
medcharrafi's avatar

Remove the www from the URL:

If a persone enter www.example.com, you want them to be redirected to example.com Ensure the use of HTTPS:

If a persone try to access your website using "http://" instead of "https://" you want them to be automatically redirected to the secure version with "https://." yeh isn't ? To do this, create or edit your .htaccess file and add the following lines:

RewriteEngine On

1)----> Redirect https://example.com/$1eCond %{HTTP_HOST} ^www. [NC] RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]`

2)------> Redirect HTTP to HTTPS RewriteCond %{HTTPS} !=on RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

3)----->Route all requests to index.php RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L] Make sure to replace "example.com" with your own domain name.

These rules will redirect visitors to the non-"www" version of your website and ensure that they are using the secure HTTPS protocol. Additionally, it will preserve the specific page or location they are trying to access.

ilex01's avatar

@medcharrafi Thank you for trying to help me. As it's hard for me to read your code, could you provide me the full code to copy and paste?

Please or to participate in this conversation.