Search code examples
php.htaccesshttphttp-redirect

While redirecting all pages with .htaccess my codes are conflicting


I am trying to redirect to

https://example.com from https://www.example.com, http://example.com, http://www.example.com

------------------ and ------------------

https://example.com/sub_page/pages from https://www.example.com/sub_page/pages, http://example.com/sub_page/pages, http://www.example.com/sub_page/pages

on .htaccess but my sub_pages are not redirecting properly. probably because of conflicting codes below. I read many articles on Internet but do not fount useful.

my .htaccess file

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]


RewriteEngine On
RewriteCond %{HTTP_HOST} !^example.com$
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

I think you got my whole point. In short, I want to redirect all requests to non=www and https. Please help me with this,


Solution

  • This probably is what you are looking for:

    RewriteEngine On
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} !^example\.com$
    RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L]
    

    That should work in the actual http server configuration or, if you have no access to that, in a distributed configuration file (".htaccess) located in the http hosts's document root.