0

I'm running a few personal websites off a ubuntu 16.04 VPS server and I'm trying to modify each site so that no ".html" will appear as the suffix to the website and that "www." will not appear as a prefix. I would like the website to work if someone types in these modifiers to the address as well. So far the removal of the www. and .html works well, but none of my individual pages are now loading. For instance, example.com/projects is a tab on my navigation bar in the example.com index. If I click the tab the address bar shows example.com/projects and a "404 not found" with "The requested URL /projects was not found on this server". Regardless if I change projects to href="projects.html" or href="projects" in the main index.html file, nothing loads and I get a 404. I think there may be something wrong with the way the directory is input into the .conf file. It should be trying to load "projects" not "/projects"

What I have tried:

Here's my example code:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    ServerAdmin admin@example.com
    DocumentRoot /var/www/example.com

    <Directory /var/www/example.com>
        Options -Indexes +FollowSymLinks
        AllowOverride All
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
    CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
    RewriteEngine on
    RewriteCond %{SERVER_NAME} =www.example.com [OR]
    RewriteCond %{SERVER_NAME} =example.com
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

<VirtualHost *:443>
    ServerName example.com
    ServerAlias www.example.com
    ServerAdmin admin@example.com
    DocumentRoot /var/www/example.com

    <Directory /var/www/example.com>
        Options -Indexes +FollowSymLinks
        AllowOverride All
    </Directory>

    RewriteEngine on 
    RewriteCond %{THE_REQUEST} \s/+(.*)\.html[\s?/] [NC]
    RewriteRule ^  /%1 [R=301,L,NE]
    RewriteCond %{REQUEST_FILENAME}   !-f 
    RewriteCond %{REQUEST_FILENAME}\.html -f
    RewriteRule ^(.*)$ /$1.html [L]

    ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
    CustomLog ${APACHE_LOG_DIR}/example.com-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>

I'm not exactly sure how to resolve this problem. If anyone has any input that would be most excellent!

Thanks a ton for any advice!

Your Answer

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

Browse other questions tagged or ask your own question.