0

I want to 301 redirect everything to https, www and index.php with .htaccess. I've tried several different combinations of code, but they all force non-www or break. I commented out the https redirect in the code below because it does not work, but I want to show an example of something I tried.

Also, with the code below, https://example.com/example-page works, but when I add a trailing / it redirects to https://example.com.

Thank you for your help.

AddType text/css .css
AddType application/x-httpd-php .php .htm .html

<IfModule mod_rewrite.c>
RewriteEngine On

# RewriteCond %{SERVER_PORT} !^443$
# RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301]

RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

</IfModule>
  • Hey check: https://stackoverflow.com/questions/13977851/htaccess-redirect-to-https-www – Anders Sørensen Sep 06 '17 at 21:09
  • What do you mean with regards to the "...and index.php" bit? The redirect to the root if you include a slash on the end of the URL, would seem to be something triggered from application logic (or somewhere else)? The code you've shown above does not do that. – MrWhite Sep 09 '17 at 21:57
  • "it does not work" - You need to explain _why_ it doesn't work. Does it do anything? Does it redirect incorrectly? Or do you get an error? In the commented code above you've omitted the `L` flag on the `RewritRule` - so, yes, this will prevent it from working correctly (it will likely redirect to `index.php`). However, since you have "tried several different combinations", I would be surprised if you have always omitted the `L` flag (since 99% of examples out there will include it)? Very rarely would you have an external redirect in `.htaccess` without the `L` (`last`) flag. – MrWhite Sep 23 '17 at 10:27

1 Answers1

0

You should use [OR] operator at the end of condition, this is a example for [OR] operator.

RewriteEngine on
RewriteCond %{HTTPS} on [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^index\.php$ - [L]

i have not checked it.

Amir Fo
  • 5,163
  • 1
  • 43
  • 51
  • your welcome, if it works, you can tick this answer. @user1678848 – Amir Fo Sep 06 '17 at 21:23
  • This won't redirect anything. You haven't specified a _substitution_ URL, (or included a 3xx redirect status). Your `RewriteCond` directive also implies you want to _remove_ the www subdomain, not add it. – MrWhite Sep 06 '17 at 22:38
  • Your answer on the other question (that you link to) appears to be the correct solution (not sure why you didn't just copy that)? (However, since the OP appears to have tried various solutions and none have worked then there may be something else going on.) – MrWhite Sep 23 '17 at 10:22
  • I changed my answer for his question type! @MrWhite – Amir Fo Sep 24 '17 at 07:08