1

I'm trying to get my redirect to work but am having trouble. The HTTP to HTTPS redirect does work, but trying to make a RESTful API is not. Any ideas would be appreciated. Thank you.

<VirtualHost *:80 *:443>
    <Directory /var/www/example.com/public_html>
        #Options +FollowSymLinks
        Options -Indexes +FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>

    ServerName www.example.com
    ServerAlias example.com
    DocumentRoot /var/www/example.com/public_html
    ErrorLog /var/www/example.com/error.log
    CustomLog /var/www/example.com/requests.log combined

    RewriteEngine On #only turn on once

    #force https
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

    <Directory "/var/www/example.com/public_html">
        <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-s
        RewriteRule ^(.*)$ /api/index.php?rquest=$1 [QSA,NC,L]

        RewriteCond %{REQUEST_FILENAME} -d
        RewriteRule ^(.*)$ /api/index.php [QSA,NC,L]

        RewriteCond %{REQUEST_FILENAME} -s
        RewriteRule ^(.*)$ /api/index.php [QSA,NC,L]
        </IfModule>

    </Directory>


</VirtualHost>

Edit: I rewrote the 2nd Directory section to look like this:

   <Directory "/var/www/example.com/public_html">
        <IfModule mod_rewrite.c>
        RewriteEngine On

        #force https
        RewriteCond %{HTTPS} off
        RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-s
        RewriteRule ^(.*)$ /api/index.php?rquest=$1 [QSA,NC,L]

        RewriteCond %{REQUEST_FILENAME} -d
        RewriteRule ^(.*)$ /api/index.php [QSA,NC,L]

        RewriteCond %{REQUEST_FILENAME} -s
        RewriteRule ^(.*)$ /api/index.php [QSA,NC,L]
        </IfModule>

    </Directory>
Christian
  • 11
  • 2
  • Move it inside the directory stanza between the Rewrite Engine on and the first Rewrite Cond. – davidgo May 06 '20 at 05:59
  • I updated the 2nd entry above. Going to "https://example.com/some/stuff" still produces a 404 error "The requested URL /some/stuff was not found on this server." I can go to the URL it's supposed to redirect to "https://example.com/api/index.php". – Christian May 06 '20 at 18:05
  • The problem seems to be with the HTTPS redirect. If I remove that directive, I can go to the site via HTTP. Even if I manually enter HTTPS in the browser, it ceases to work. – Christian May 07 '20 at 16:23
  • From what you have said, it sounds like the problem is not the redirect at all. Https needs to work before the redirect is handled. What happens if you go to the https version of the site without the redirect? Also, where are the SSL certificate and config lines, and why do you have this config wrapped in virtualhost tags? How many vhosts do you have? (There are times this is correct, but it is a bit unusual, and may indicate the problem is inappropriate use a single virtualtag block) – davidgo May 07 '20 at 18:49
  • I used to host multiple domains on the server but now just the one, hence the virtual host. SSL is handled by LetsEncrypt/certbot. I can go to any file that actually exists, via HTTPS. – Christian May 07 '20 at 20:51

0 Answers0