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

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 webmaster#example.com
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.

Thanks to anlutro in #laravel IRC chat, he pointed out that mod_rewrite may not be enabled. 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. All is good.
Cheers

Related

laravel5.1 laravel in subfolder (routes not working) results in 404 apache error

My current setup on my production server is like the following:
there are two folders in
var/www/html
These two folders are named:
xyz
doge
Now I would like to place all requests to IP/doge into the doge folder.
This seems to work, but only the index page is working. Using the already created routes like for example:
ip/doge/login
will result in a 404 error page of apache.
Here's my apache config:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html/xyz/public
Alias "/doge" "/var/www/html/doge/public"
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /var/www/html/xyz/public/>
AllowOverride All
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
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>
Any idea where's my fault? I haven't changed the .htaccess in the public folder of each laravel project. This .htaccess looks like:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>

Virtual host setup < cgi script

The issue
I'm writing a cgi script in c++. All works well as long as I go in to the precise url: http://localhost:90/joppli.bot
Now I'm trying to make a simple redirect to from everything to my cgi script. eg:
http://localhost:90/
http://localhost:90/foo
http://localhost:90/foo/bar
...should all render the same content as entering http://localhost:90/joppli.bot
Files
.htaccess
RewriteEngine On
RewriteRule (.+)/$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ joppli.bot [NC,L]
virtual host
<VirtualHost *:90>
DocumentRoot /var/www/joppli-bot
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
LoadModule fastcgi_module /usr/lib/apache2/modules/mod_fastcgi.so
SetHandler fastcgi-script
<Directory "/var/www/joppli-bot">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog /var/www/joppli-bot/log/apache2/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog /var/www/joppli-bot/log/apache2/access.log combined
</VirtualHost>
files at /var/www/joppli-bot
.htaccess
joppli.bot -> /home/erik/NetBeansProjects/joppli.bot/dist/Debug/GNU-Linux-x86/joppli.bot
log
+ apache2
+ access.log
+ error.log
Thanks to Justin Iurman Who wrote the following as a comment, witch solved most of it:
In <Directory "/var/www/joppli-bot"> block, replace AllowOverride None
by AllowOverride All (otherwise your htaccess is disabled)
I also added DirectoryIndex joppli.bot at the bottom of my .htaccess file to route all trafic from root directory to the script as well..

htaccess vhost codeigniter index.php

I have read a lot of topic with the same problem but i do not find the solution... please help me. I have a lamp into ubuntu server. My document root is /home/utente/ into this dir I have another dir (turni) with a codeigniter web app. The web app works fine with the "index.php" into the url, but I want to eliminate it. I have this configuration:
config.php into codeigniter:
$config['index_page'] = '';
.htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
/etc/apache2/sites-available/default:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /home/utente
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /home/utente/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
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
Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
When i open a link of the web app with without eh "index.php" into the url, the server o me this error: The requested URL /turni/auth/login was not found on this server.
Why??? If i put the index.php like /turni/index.php/auth/login all works fine....
Thanks for help and sorry for my english :D
Add .htaccess to your server root directory and change the rewrite base to
RewriteBase /turni
assuming your codeigniter is present in /var/www/turni

apache redrict 301 from https://www.domain.com to https://domain.com

im trying for hours to redrict https:// www. domain .com to https:// domain .com with 301.
I have a certificate for domain .com but no wildcard for www. domain .com.
I tried the following:
RewriteEngine On
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^www\.meinedomain\.de$ [NC]
RewriteRule ^(.*) https://meinedomain.de/$1 [L,R=301]
and
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
and
Redirect 301 / https://mineyourmind.de/
None of this works. Any ideas what could be wrong? I have added this directly to the apache settings and it is working great for http:// www. domain .com and http:// domain .com to https:// domain .com but not for https:// www. domain .com.
At the moment I use the following config. Every redrict works fine, instead of the https://www.mineyourmind.de to https://mineyourmind.de
How should such a file look like ?
<VirtualHost *:80>
ServerName mineyourmind.de
ServerAlias www.mineyourmind.de
Redirect 301 / https://mineyourmind.de/
</VirtualHost>
<VirtualHost *:443>
ServerAdmin **
DocumentRoot /var/www/mineyourmind
ServerName mineyourmind.de
ServerAlias www.mineyourmind.de
SSLEngine on
SSLCertificateFile /etc/**
SSLCertificateKeyFile /etc/**
SSLCertificateChainFile /etc/**
SSLCACertificateFile /etc/**
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
deny from all
</Directory>
<Directory /var/www/mineyourmind/>
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>
Second Edit:
<VirtualHost *:80>
ServerName mineyourmind.de
ServerAlias www.mineyourmind.de
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.mineyourmind\.de$ [NC]
RewriteRule (.*) https://mineyourmind.de%{REQUEST_URI} [R=301,L]
</VirtualHost>
<VirtualHost *:443>
ServerAdmin **
DocumentRoot /var/www/mineyourmind
ServerName mineyourmind.de
ServerAlias www.mineyourmind.de
SSLEngine on
SSLCertificateFile /etc/apache2/**
SSLCertificateKeyFile /etc/apache2/ssl/**
SSLCertificateChainFile /etc/apache2/ssl/**
SSLCACertificateFile /etc/apache2/ssl/**
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.mineyourmind\.de$ [NC]
RewriteRule (.*) https://mineyourmind.de%{REQUEST_URI} [R=301,L]
<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
deny from all
</Directory>
<Directory /var/www/mineyourmind/>
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>
If you request https://www.meinedomain.de/ and your cert isn't authorized for www.meinedomain.de, then you'll get a security warning regardless of how you redirect. The redirect can only happen after they accept the security exception.
Make sure you've put those rules in the SSL vhost's config, not the non-SSL one. Apache usually has them separate. If that still doesn't work, you can try just putting this in an htaccess file in your document root:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.meinedomain\.de$ [NC]
RewriteRule (.*) https://meinedomain.de%{REQUEST_URI} [R=301,L]

Apache Rewrite rules not working as expected

I have two apache servers both have identical settings, I cloned the apache config files, and changed the ServerName part only. When I type mysite.com/somestuff it should rewrite to index.php it does it on my old server, but not my new. I have made sure the .htaccess is there when I do mysite.com/index.php/somestuff it works, but like my first site I need it to work with mysite.com/somestuff.
I am really banging my head against the wall here is my .htaccess and apache config file
#.htaccess file
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(/(.*)|$) %{CONTEXT_PREFIX}/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule .? %{ENV:BASE}index.php/ [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
RedirectMatch 302 ^/$ /index.php/
</IfModule>
</IfModule>
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>
Now for my apache config
<VirtualHost *:80>
ServerAdmin user#host.com
ServerName mysite.com
DocumentRoot /home/richardw/www/halogen/web
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /home/richardw/www/halogen/web/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
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>
My apologizes if this is a repost, but I've been searching all over and i'm about to lose it.
When you go to http://mysite.com/, are you redirected to http://mysite.com/index.php/?
If this is happening, that means mod_rewrite is not loaded in your new server. You need to make sure it's loaded in your apache's server config file. See this answer for some instructions on how that works for apache.
The reason why the redirect is working is because of this container:
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
RedirectMatch 302 ^/$ /index.php/
</IfModule>
</IfModule>
This essentially says "if mod_rewrite is not loaded", then if mod_alias is loaded, it redirects the root request to /index.php/. So if the redirect is happening, mod_rewrite is not loaded.
Have you try to comment or remove this line
Options +FollowSymlinks
in your .htaccess?