Search code examples
apachehttp-redirectubuntularavel-4virtualhost

Remove index.php from Laravel 4 URL and Rewrite www to non-www


I have tried several ways to remove index.php from my URL (http://www.example.com/index.php/login), as well as redirecting www to the non-www URL.

Here is my default Apache VirtualHost file:

<VirtualHost *:80>
        ServerAdmin [email protected]
        ServerName example.com
        ServerAlias www.example.com

        DocumentRoot /var/www/public/
        <Directory />
                Options FollowSymLinks
                AllowOverride All
        </Directory>
        <Directory /var/www/public/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Now I have added the following in to this file:

Redirect 301 / http://example.com

But this has not worked, I just get a redirect loop message. I also have the following .htaccess:

<IfModule mod_rewrite.c>
    Options -MultiViews
    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

To which I've added:

RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

But this does not work either.

The other issue is that for all of my routes I have to include index.php, like http://example.com/index.php/test

It's as if my .htaccess file is being ignored.

The server is a DigitalOcean Droplet running Ubuntu 13.04 x64. It has PHP 5.4.9 and Apache 2.2.22.

Any help would be very much appreciated. Thanks.


Solution

  • mod_rewrite was not enabled on my server. Usually this is on by default in all the servers I've ever worked with so it didn't click, as I've never had to enable it but on this DigitalOcean server, it wasn't.

    I used a2enmod rewrite to enable it and then I restarted Apache.