skip to Main Content

Every thing is working fine including HTTP to https redirection on browsers

http://example.com/app/login  404
https://example.com/app/login 404
http://www.example.com/app/login 404
https://www.example.com/app/login 404

http://example.com/index.php/app/login  OK
https://example.com/app/index.php/login OK
http://www.example.com/index.php/app/login OK
https://www.example.com/index.php/app/login OK

URLs tested with postman

http to https redirection works fine respecting www or non www

Server
Ubuntu 18.04
Apache 2.4.29

/etc/hosts  

 206.189.85.* example.com #server ip address
 206.189.85.* www.example.com
 127.0.0.1 localhost

# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

/etc/apache2/sites-available/example.com.conf

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName www.example.com
    ServerAlias example.com
    DocumentRoot /var/www/example.com/public

    <Directory /var/www/example.com/public/>
        Options -Indexes +FollowSymLinks +MultiVies
        AllowOverride All      
        Require all granted     
     </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    LogLevel warn
</VirtualHost>

/etc/apache2/sites-available/example.com-le-ssl.conf

<IfModule mod_ssl.c>
<VirtualHost *:443>
    ServerAdmin [email protected]
    ServerName www.example.com
    ServerAlias example.com
    DocumentRoot /var/www/example.com/public

    SSLEngine On

    <Directory /var/www/html/example.com/public/>
        Options Indexes FollowSymLinks
        AllowOverride All       
        Require all granted
     </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
</VirtualHost>
</IfModule>

.htaccess content

DirectoryIndex index.php
<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>


    RewriteEngine On



    RewriteCond %{HTTPS} off   
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

    RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::2$
    RewriteRule ^(.*) - [E=BASE:%1]

    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^index.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]

    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule .? - [L]

    RewriteRule .? %{ENV:BASE}/index.php [L]

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

</IfModule>

3

Answers


  1. add DirectoryIndex & Redirect permanent to your virtualhost:80

    <VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName www.example.com
    ServerAlias example.com
    DirectoryIndex index.php
    DocumentRoot /var/www/example.com/public
    Redirect permanent / https://www.example.com/
    
    <Directory /var/www/example.com/public/>
        Options -Indexes +FollowSymLinks +MultiVies
        AllowOverride All      
        Require all granted     
     </Directory>
    
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    LogLevel warn
    </VirtualHost>
    
    Login or Signup to reply.
  2. check list

    1. is your apache a2enmod rewrite enabled ?

      command to enable

      sudo a2enmod rewrite

      sudo service apache2 restart

    2. add this line in end .htaccess

      RewriteRule . ^ index.php [L]

      check this thread for more info
      Thread

    Login or Signup to reply.
  3. Can you change
    RewriteRule .? %{ENV:BASE}/index.php [L]
    to
    RewriteRule ^ index.php [L]

    Edit:

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
    

    Can you check with this Rewrite rules. If working add your https and authentication conditions later.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search