1

I recently set up a vps with digital ocean to let my site run on php7 and http2 Everything is running great but my sites .htaccess file is not redirecting as it should. I have changed up my 000-default.conf and my apache2.conf files to allow .htaccess to work!

This is what I have in my .htaccess file

#ExpiresActive On
#ExpiresByType image/gif "now plus 9 years"
#ExpiresByType image/png "now plus 9 years"
#ExpiresByType image/jpeg "now plus 9 years"
#ExpiresByType image/x-icon "now plus 9 years"

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule .* https://example.com [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.example.com [L,R=301]

RewriteBase /
RewiteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$1 [R=301,NE,L]

On my other shared hosting this file worked fine and I know this file is working because if I uncomment all of the ExpiresByType it breaks my site so I will probably take those out! Also the index.php does not redirect. Have I missed something somewhere?

Have been told that my question is unclear so will provide my sites .conf file.

DirectoryIndex index.php
<LocationMatch "^(.*\.php)$">
    ProxyPass fcgi://127.0.0.1:9000/var/www/example.com/public_html
</LocationMatch>
NameVirtualHost *:443
<VirtualHost *:443>
    AccessFileName .htaccess
    ServerName www.example.com
    ServerAlias www.example.com

    SSLEngine on
    SSLCertificateFile "/etc/letsencrypt/live/example.com/cert.pem"
    SSLCertificateKeyFile "/etc/letsencrypt/live/example.com/privkey.pem"
    SSLCertificateChainFile "/etc/letsencrypt/live/example.com/chain.pem"

    Protocols h2 http/1.1
    H2Direct on
    ServerAdmin phillip@example.com
    DocumentRoot /var/www/example.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    <Directory /var/www/>
            Options Indexes FollowSymLinks MultiViews Includes
            AllowOverride All
            Order allow,deny
            Allow from all
    </Directory>
</VirtualHost>

I can also confirm that sudo a2enmod rewrite is enabled. Hope this clears things up.

  • Sorry but as it stands the question is currently considered unclear because of the lack of information provided. In order for our community to provide a solid answer please provide the conf from within /sites-enabled/ and details if you have or not enabled mod_write using sudo a2enmod rewrite. – Simon Hayter Feb 19 '16 at 17:43
  • NP Will do Simon, will edit my question now – Phillip Dews Feb 19 '16 at 17:51
  • All done @SimonHayter hope this helps – Phillip Dews Feb 19 '16 at 18:00
  • You don't actually state what is "not redirecting properly" (apart from the "Also index.php..." part). So, it is redirecting, but not as expected? Can you include what URL(s) you are requesting and what output you are expecting and what output you are currently getting. – MrWhite Feb 19 '16 at 19:38
  • ah no problem @w3dk I am expecting all versions to redirect to example.com presently if i goto example.com or example.com or example.com it stays there so naturally I want them all to goto example.com. Hope this helps? – Phillip Dews Feb 19 '16 at 20:35
1

I finally figured it out and it was really simple. Instead of modifying my sites virtual hosts I file modified my apache2.conf file located at /etc/apache2/apache2.conf as such

<Directory /var/www>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

Took me a while to get there but simple when you know how! Thanks for all your help

  • Thanks for the feedback. However, you shouldn't have to put these directives directly in apache2.conf - which controls your entire server. You've used Require all here, which suggests you're on Apache 2.4, whereas in your question you used the Apache 2.2 Order / Allow directives - this could have perhaps indicated a conflict? – MrWhite Feb 26 '16 at 1:18
  • Ah yea, Thanks for that @w3dk for that! I just added sendmail to my vps as well and thats not working so it probaly already has caused a conflict – Phillip Dews Feb 26 '16 at 13:41

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.