skip to Main Content

Known 404 requests are not being redirected for https… and only display a generic Not Found 404 … http requests on port 80 are begin rewritten just fine…

Notes: Using http.conf files (not .htaccess) on a Apache Server (2.4.39) spun up through AWS elastic beanstalk on 64bit Amazon Linux/2.8.9.

Example:

  • example.com/fake
  • sub.example.com/fake

Not Found

The requested URL /fake was not found on this server.

.

My config file has 2 parts…

Part 1 redirects http to https… this RewriteRule works perfectly…

  <VirtualHost *:80>
    ServerName sub.example.com
    DocumentRoot /var/www/html/sub
    RewriteEngine On
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteRule . https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
  </VirtualHost>

Part 2 is supposed to rewrite sub.example.com/fake to sub.subdomain.com/?url=$1

  <VirtualHost *:443>
    ServerName sub.example.com
    DocumentRoot /var/www/html/sub
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php?url=$1 [L]
  </VirtualHost>

I thought the rewrite rule was the part that is miss-written, but it’s not because even the following attempts did not work.

  <VirtualHost *:443>
    ServerName sub.example.com
    DocumentRoot /var/www/html/sub
    ErrorDocument 404 "No go bro..."
  </VirtualHost>

I thought is was only an issue with sub domains but this didn’t work either.

  <VirtualHost *:443>
    ServerName example.com
    DocumentRoot /var/www/html/www
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ http://google.com [L]
    ErrorDocument 404 "No go bro 1..."
  </VirtualHost>

  <VirtualHost *:443>
    ServerName sub.example.com
    DocumentRoot /var/www/html/sub
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ http://google.com [L]
    ErrorDocument 404 "No go bro 2..."
  </VirtualHost>

Tried adding overrides as suggested, but that didn’t work

  <VirtualHost *:443>
    <Directory /var/www/html/www>
      AllowOverride All
      Allow From All
    </Directory>
    ServerName ecample.com
    DocumentRoot /var/www/html/www
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ http://google.com [L]
    ErrorDocument 404 "No go bro..."
  </VirtualHost>

One reference said the RewriteRules needed to be in Directory tags within the VirtualHost tags… so I tried that with no luck

  <VirtualHost *:443>
    ServerName example.com
    DocumentRoot /var/www/html/www
    <Directory /var/www/html/www>
      Allow From All
      AllowOverride All
      RewriteEngine On
      RewriteCond %{REQUEST_FILENAME} !-f [OR]
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^(.*)$ /index.php?url=$1 [L]
      ErrorDocument 404 "No go bro..."
    </Directory>
  </VirtualHost>

The DocumentRoot paths are correct as all known urls example.com & sub.example.com work fine just 404 is where there is a problem.

2

Answers


  1. Chosen as BEST ANSWER

    The rewrite rules need to put put under the <VirtualHost *:80> before the https redirect. Why... not sure. But that's the only way it works.

    <VirtualHost *:80>
      ServerName sub.example.com
      DocumentRoot /var/www/html/sub
      RewriteEngine On
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^(.*)$ /index.php?url=$1 [L]
      RewriteCond %{HTTP:X-Forwarded-Proto} !https
      RewriteRule . https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
    </VirtualHost>
    

  2. Put your Redirect config there:

    <VirtualHost *:80>
        ServerName blog.example.com
        Redirect / http://example.com/blog/
    </VirtualHost>
    

    Write this code on your .htaccess file.

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^([^.]+).example.com$
    RewriteRule ^/(.*)$ http://example.com/%1/$1 [R=301,L]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search