1

My site with my own PHP framework allows to have nice urls for pages:

About Us  =>  https://www.myurl.com/about
Contacts  =>  https://www.myurl.com/contacts

It works well in Vagrant local setup, but once I upload my site to Apache2 going to those sites throws:

The requested URL /about was not found on this server

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

Probably because it's priority is to search for a file inside of "about" and "contacts" folders instead of listen to /index.php file that defines routes and loads views for about or contact pages.

View in https://www.myurl.com homepage works perfectly, the problem occurs going to pages declared in URI after https://www.myurl.com/.....

This is my .conf file

ServerAdmin info@myurl.com
ServerName myurl.com
ServerAlias www.myurl.com
DocumentRoot /var/www/myurl.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

RewriteEngine on
RewriteCond %{SERVER_NAME} =www.myurl.com [OR]
RewriteCond %{SERVER_NAME} =myurl.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI}      [END,NE,R=permanent]
  • 1
    Please edit the question and post the content of your virtual host .conf file. – pa4080 Sep 7 '17 at 8:41
  • 2
    And under /var/www/myurl.com/public_html has two sub folder about/ and contacts/ right? I think the configuration file must be more detailed, also there should be <Directory> definitions and, also, you can avoid Rewrite rules for this task. I will post my preferred configuration for this case. – pa4080 Sep 7 '17 at 8:57
  • @pa4080 No, there are no subfolders about/, contacts/, the configuration in /var/www/myurl.com/public_html/index.php page is made that: if user goes to url: myurl.com/about, the code displays a html code that is stored in /var/www/myurl.com/public_html/views/about-us.view.php – Lukas Sep 7 '17 at 9:00
1

After adding these lines to my .htaccess file the routing was finally working:

Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

Before when I was going to https://www.myurl.com/about it was searching for a index.php file inside of /var/www/myurl.com/public_html/about/ directory.

With new code in .htaccess when I go to https://www.myurl.com/about the .htaccess file first prioritises the search for the view as it's defined in index.php file and if it doesn't load any view, it searches for a directory with a given name.

  • I was just thinking about that you need RewriteRule as this :) Because I couldn't imagine how the php program will handle the requests that don't point to it. – pa4080 Sep 7 '17 at 10:41
1

I don't have an exact idea why the above configuration works on Vagrant but not on native Apache. My experience show me that, when the Apache's configuration is not strict enough, it works sometimes and sometimes it doesn't work. Especially when you have migrated from one to other Apache's version. Also several times I've read that it is preferable to avoid using of Rewrite rules if it is possible.

So I would prefer to setup my virtual host with separate configuration file, that should look like that:

ServerName myurl.com # Remove this line if there is another ServerName directive at the global level, or change it to 'ServerName localhost' if you want.

<VirtualHost *:80>  
        ServerName myurl.com
        ServerAlias www.myurl.com
        ServerAdmin info@myurl.com
        ErrorLog ${APACHE_LOG_DIR}/myurl.com.error.log
        CustomLog ${APACHE_LOG_DIR}/myurl.com.access.log combined   

        # Redirect Requests to HTTPS
        Redirect permanent "/" "https://myurl.com/"
</VirtualHost>

<IfModule mod_ssl.c>    
<VirtualHost _default_:443>
        ServerName myurl.com
        ServerAlias www.myurl.com
        ServerAdmin info@myurl.com
        ErrorLog ${APACHE_LOG_DIR}/myurl.com.error.log
        CustomLog ${APACHE_LOG_DIR}/myurl.com.access.log combined   

        DocumentRoot /var/www/myurl.com/public_html
        <Directory /var/www/myurl.com/public_html>
               Options None FollowSymLinks
               AllowOverride None 
               # Use 'AllowOverride All' to enable all .htaccess overrides
               DirectoryIndex index.html index.php
               Order allow,deny
               Allow from all
               Require all granted
        </Directory>

        SSLEngine on
        SSLCertificateFile /path/to/your/cert.file
        SSLCertificateKeyFile /path/to/your/privkey.file
        SSLCertificateChainFile /path/to/your/chain.file
</VirtualHost>
</IfModule>

Create new configuration file: /etc/apache2/sites-available/myurl.com.conf. Where you should paste and modify the above lines.

Disable the current configuration file. Probably you should use a2dissite. Then enable the new configuration file: sudo a2ensite myurl.com.conf. And restart Apache. I think it should work now.

Additionally, within the above configurations booth http://myurl.com and http://www.myurl.com will redirect all requests to https://myurl.com. However the address https://www.myurl.com will be accessible when it is requested. If you want redirections as these:

  • http://myurl.com to https://myurl.com and

  • http://www.myurl.com to https://www.myurl.com.

You should remove the directive ServerAlias from the <VirtualHost *:80> definition and create another one:

<VirtualHost *:80>  
        ServerName myurl.com
        ...
        Redirect permanent "/" "https://myurl.com/"
</VirtualHost>

<VirtualHost *:80>  
        ServerName www.myurl.com
        ...
        Redirect permanent "/" "https://www.myurl.com/"
</VirtualHost>

...
  • 1
    Thank you very much for your answer, I studied it a lot, before discovering my problem. – Lukas Sep 7 '17 at 10:37

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.