3

My rewrite rules are designed to hide my directory structure and to make URLs easier to read. The gcems site works as expected while the vigotwpfd site will not load the files beyond the first page as well as throwing an error on the first page.

Here is my setup:

Development platform: Both sites work as intended.

"Live / Testing" platform: One machine running one instance of Apache 2.4 on Linux.

One dynamic IP connection to the internet.

Two host names, both using duckdns.org as my dynamic IP provider. gcems.duckdns.org and vigotwpfd.duckdns.org setup as virtual hosts.

Both sites are running PHP 7.x

Both sites are setup very similar. Same directory structure. File structures are similar. Both use URL re-writing rules, which are identical with the exception that the URL rules for gcems point to gcems and the rules for vigotwpfd point to vigotwpfd.

The Apache domain config files and .htacess files are also identical, with the exception of gcems vs vigotwpfd.

The following is a simplified directory / file structure for both domains.

  • vigotwpfd ==> /var/www/vigotwpfd
  • gcems ==> /var/www/html

/var/www/site/
|-- assets
|   |-- css
|   |   `-- view.css
|   |-- gallery
|   |   `-- blank.jpg
|   |-- htc
|   |   `-- iepngfix.htc
|   |-- images
|   |   `-- <image files>
|   `-- js
|       `-- <javascript files>
|-- classes
|   |-- Bootstrap.php
|   |-- Controller.php
|   |-- Messages.php
|   `-- Model.php
|-- controllers
|   `-- <controller files>
|-- models
|   `-- <models files>
|-- views
|   |-- <other directories and php web pages>
|   `-- main.php
|-- config.php
|-- .htaccess
`-- index.php

.htaccess

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([a-zA-Z]*)/?([a-zA-Z]*)?/?([a-zA-Z0-9]*)?/?$ index.php?controller=$1&action=$2&id=$3 [NC,L]
Options +FollowSymLinks
RewriteEngine on

RewriteCond %{SERVER_PORT} 80
# This checks to make sure the connection is not already HTTPS -  port 80 indicates a non-secured HTTP conection.

RewriteRule ^(.*)$ https://site.duckdns.org/$1 [R=301,L]
# This rule will redirect users from their original location to the same location but using HTTPS.

RewriteRule ^([a-zA-Z]*)/?([a-zA-Z]*)?/?([a-zA-Z0-9]*)?/?$ index.php?controller=$1&action=$2&i$

apache2/sites-enabled/gcems.duckdns.org.conf

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerName gcems.duckdns.org
        ServerAlias *.gcems.duckdns.org
        DocumentRoot /var/www/html

        ErrorLog ${APACHE_LOG_DIR}/gcems_error.log
        CustomLog ${APACHE_LOG_DIR}/gcems_access.log combined
</VirtualHost>

error message found in vigotwpfd_error.log

[Mon Nov 05 08:33:40.101435 2018] [:error] [pid 2993] [client 107.77.210.136:29694] PHP Notice:  Undefined index: controller in /var/www/vigotwpfd/classes/Bootstrap.php on line 38, referer: https://vigotwpfd.duckdns.org/
[Mon Nov 05 08:33:40.101670 2018] [:error] [pid 2993] [client 107.77.210.136:29694] PHP Stack trace:, referer: https://vigotwpfd.duckdns.org/
[Mon Nov 05 08:33:40.101784 2018] [:error] [pid 2993] [client 107.77.210.136:29694] PHP   1. {main}() /var/www/vigotwpfd/index.php:0, referer: https://vigotwpfd.duckdns.org/
[Mon Nov 05 08:33:40.101914 2018] [:error] [pid 2993] [client 107.77.210.136:29694] PHP   2. Bootstrap->__construct() /var/www/vigotwpfd/index.php:60, referer: https://vigotwpfd.duckdns.org/
[Mon Nov 05 08:33:40.102365 2018] [:error] [pid 2993] [client 107.77.210.136:29694] PHP Notice:  Undefined index: action in /var/www/vigotwpfd/classes/Bootstrap.php on line 45, referer: https://vigotwpfd.duckdns.org/
[Mon Nov 05 08:33:40.102488 2018] [:error] [pid 2993] [client 107.77.210.136:29694] PHP Stack trace:, referer: https://vigotwpfd.duckdns.org/
[Mon Nov 05 08:33:40.102556 2018] [:error] [pid 2993] [client 107.77.210.136:29694] PHP   1. {main}() /var/www/vigotwpfd/index.php:0, referer: https://vigotwpfd.duckdns.org/
[Mon Nov 05 08:33:40.102630 2018] [:error] [pid 2993] [client 107.77.210.136:29694] PHP   2. Bootstrap->__construct() /var/www/vigotwpfd/index.php:60, referer: https://vigotwpfd.duckdns.org/

https://github.com/pavulon18/vigotwp
https://github.com/pavulon18/gcas_timesheet_php_website

Here are my theories:

  1. There is an issue in the URL rewrite system
  2. There is an issue in the apache configs (either for the vigotwpfd site or the server itself)
  3. My code is messed up.

I have run vigotwpfd step by step through a debugger but I have yet to find out where my problem is.

Additional information and clarifications

In the first iteration of my question, I was trying to save some room and reduce some noise by posting that which I thought was absolutely essential. I will post the whole contents of the files in this edit. I am also going to answer some of the questions and concerns that were presented.

Q) "but the error log you posted would seem to suggest that your URL rewrite is not being processed at all? – MrWhite"
A) This is one of the big puzzles of this whole situation. This setup, as given, works on the gcems site, but not on the vigotwpfd site. I am trying to determine what the difference is between the two.

Q) "The directives in the .htaccess file you’ve posted are in the wrong order and you seem to have (sort of) duplicated your main rewrite? (Btw, you’ve referred to “htaccess” with one “c” earlier in your question - I assume this is just a typo in your question?) – MrWhite yesterday"
A) I will fix the duplication that you mention. For this update, I opted to keep the original just to keep from significantly changing the premise of the question. Yes, the single "c" was a simple typo. I double-triple checked the file name after you mentioned this error.

Comment: MrWhite, I noticed that you removed my PHP tag. The reason I listed that is because of the possibility that the error is somewhere in my PHP code. I move that the PHP tag should be reinstated.

on gcems: if I type https://gcems.duckdns.org, I get the main page of the website, with no error messages. if I type https://gcems.duckdns.org/some_random_characters, the website will dispay an error message "Controller Class does not exist". My code catches the wrong directory/file request and displays this message instead of the 404 error.

on vigotwpfd: If I type https://vigotwpfd.duckdns.org, I get the error message listed above. If I type https://vigotwpfd.duckdns.org/some_random_characters, I get an apache error "Not Found. The requested URL /some_random_characters was not found on this server."

/etc/apache2/sites-enabled/gcems.duckdns.conf

<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.

    ServerAdmin webmaster@localhost
    ServerName gcems.duckdns.org
    # ServerAlias *.gcems.duckdns.org
    DocumentRoot /var/www/html

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/gcems_error.log
    CustomLog ${APACHE_LOG_DIR}/gcems_access.log combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

/etc/apache2/sites-enabled/gcems.duckdns.org.ssl.conf

<VirtualHost *:80>
        ServerName gcems.duckdns.org
        <Location />
                Redirect permanent / https://gcems.duckdns.org/
        </Location>
</VirtualHost>
<VirtualHost *:443>
        ServerAdmin webmaster@gcems.duckdns.org
        ServerName gcems.duckdns.org
        DocumentRoot /var/www/html
        ErrorLog ${APACHE_LOG_DIR}/gcems_error.log
        CustomLog ${APACHE_LOG_DIR}/gcems_access.log combined
        SSLEngine On
        SSLCertificateFile <directory>/fullchain.pem
        SSLCertificateKeyFile <directory>/privkey.pem
</VirtualHost>

/var/www/html/.htaccess

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([a-zA-Z]*)/?([a-zA-Z]*)?/?([a-zA-Z0-9]*)?/?$ index.php?controller=$1&action=$2&id=$3 [NC,L]
Options +FollowSymLinks
RewriteEngine on

RewriteCond %{SERVER_PORT} 80
# This checks to make sure the connection is not already HTTPS -  port 80 indicates a non-secured HTTP conection.

RewriteRule ^(.*)$ https://gcems.duckdns.org/$1 [R=301,L]
# This rule will redirect users from their original location to the same location but using HTTPS.

RewriteRule ^([a-zA-Z]*)/?([a-zA-Z]*)?/?([a-zA-Z0-9]*)?/?$ index.php?controller=$1&action=$2&i$

<IfModule mod_expires.c>
  ExpiresActive ON
  ExpiresDefault "access plus 1 seconds"
</IfModule>

/etc/apache2/sites-enabled/vigotwpfd.duckdns.org.conf

<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.

    ServerAdmin webmaster@localhost
    ServerName vigotwpfd.duckdns.org
    # ServerAlias *.vigotwpfd.duckdns.org
    DocumentRoot /var/www/vigotwpfd

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/vigo_error.log
    CustomLog ${APACHE_LOG_DIR}/vigo_access.log combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

/etc/apache2/sites-enabled/vigotwpfd.duckdns.org.ssl.conf

<VirtualHost *:80>
    ServerName vigotwpfd.duckdns.org
    <Location />
            Redirect permanent / https://vigotwpfd.duckdns.org/
    </Location>
</VirtualHost>

<VirtualHost *:443>
    ServerAdmin webmaster@localhost
    ServerName vigotwpfd.duckdns.org
#       ServerAlias *.vigotwpfd.duckdns.org
    DocumentRoot /var/www/vigotwpfd

    ErrorLog ${APACHE_LOG_DIR}/vigo_error.log
    CustomLog ${APACHE_LOG_DIR}/vigo_access.log combined

    SSLEngine On
    SSLCertificateFile /var/lib/dehydrated/certs/vigotwpfd.duckdns.org/fullchain.pem
    SSLCertificateKeyFile /var/lib/dehydrated/certs/vigotwpfd.duckdns.org/privkey.pem

</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

/var/www/vigotwpfd/.htaccess

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([a-zA-Z]*)/?([a-zA-Z]*)?/?([a-zA-Z0-9]*)?/?$ index.php?controller=$1&action=$2&id=$3 [NC,L]
Options +FollowSymLinks
RewriteEngine on
LogLevel alert rewrite:trace3

RewriteCond %{SERVER_PORT} 80
# This checks to make sure the connection is not already HTTPS -  port 80 indicates a non-secured HTTP conection.

RewriteRule ^(.*)$ https://vigotwpfd.duckdns.org/$1 [R=301,L]
# This rule will redirect users from their original location to the same location but using HTTPS.

RewriteRule ^([a-zA-Z]*)/?([a-zA-Z]*)?/?([a-zA-Z0-9]*)?/?$ index.php?controller=$1&action=$2&i$

<IfModule mod_expires.c>
  ExpiresActive ON
  ExpiresDefault "access plus 1 seconds"
</IfModule>
  • 2
    You seem to have posted the .htaccess of the gcems site. What is in the .htaccess of the site that is not redirecting to https, vigotwpfd? – Michael Hampton Nov 5 '18 at 20:32
  • 1
    Please include some example URLs that are being requested (and presumably failing). You’ve only include the vHost for port 80. The directives in the .htaccess file you’ve posted are in the wrong order and you seem to have (sort of) duplicated your main rewrite? (Btw, you’ve referred to “htaccess” with one “c” earlier in your question - I assume this is just a typo in your question?) – MrWhite Nov 6 '18 at 0:22
  • 1
    To add... what URL are you requesting that results in the error log output you've quoted? Your vHost:80 container does not look complete, where is the AllowOverride directive? You say you've run this "step by step through a debugger", but the error log you posted would seem to suggest that your URL rewrite is not being processed at all? – MrWhite Nov 6 '18 at 10:45
  • I added some new information and addressed some questions. I did not alter my original post. – Jim Baize Nov 7 '18 at 17:28
  • @MrWhite -- As I mentioned in my edit, the reason, I listed PHP in my tag list is because of the possibility of my code being the source of the problem. Because of this, I move that the PHP tag be reinstated. – Jim Baize Nov 7 '18 at 17:29
0

You seem to have two <VirtualHost *:80> containers for the same ServerName (for both gcems and vigotwpfd)? Only one of these can possibly succeed - the first one that is defined in the server config (after the includes have been processed). So, I suspect the redirect from HTTP to HTTPS is not occurring in your server config, which is why you have a mod_rewrite HTTP to HTTPS redirect in your .htaccess file.

... where is the AllowOverride directive?

Since you have posted your entire(?) vHost config, there is still no AllowOverride directive anywhere in your config. Your .htaccess rewrites/redirects will not work without an appropriate AllowOverride directive somewhere in your server config (in a <Directory> container). And the PHP errors (and Apache 404) you are getting are consistent with .htaccess (ie. mod_rewrite) not doing anything.

Does http://vigotwpfd.duckdns.org redirect to HTTPS? (I just checked - it doesn't) This further suggests that .htaccess/mod_rewrite is not doing anything.

Now, you don't have an AllowOverride directive for either host, yet gcems is "working OK". It's possible you have a stray <Directory /var/www/html> container in your main server config that is enabling this for gcems, but not for /var/www/vigotwpfd? Either way, this should be defined in the vHost, not in the main server config.

PHP Notice: Undefined index: controller in ...

If the rewrite was processed in .htaccess then you wouldn't get this "PHP Notice" since $_GET['controller'] would always be defined. (Which I assume you must be referring to somewhere in your PHP code?) However, your PHP code should always be checking that the URL parameters exists anyway and defaulting if not. So, you should not be seeing this E_NOTICE under any circumstances.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.