Laravel: Apache alias for URLs with trailing slash - apache

I have a Laravel website in directory /home/user/Documents/laravel-training.
I would like to visit my website from http://localhost/dev/,
so I set an alias in /etc/apache2/sites-enabled/000-default.conf.
Here is my 000-default.conf:
<VirtualHost *:80>
ServerName foo.example.net
ServerAdmin foo#example.org
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /dev /home/user/Documents/laravel-training/public
<Directory /home/user/Documents/laravel-training/public/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
I also added RewriteBase /dev to the beginning of /home/user/Documents/laravel-training/public/.htaccess.
Now, I can visit my website successfully in case the URL does not ends with a slash.
For example, if I add the following route:
Route::get('test', function() {
return view('welcome');
});
Then I can visit it from http://localhost/dev/test.
But if my URL is http://localhost/dev/test/, the server will incorrectly redirect my page to http://localhost/test, and causes a 404 Not Found error.
Not Found
The requested URL /test was not found on this server.
Apache/2.4.12 (Ubuntu) Server at localhost Port 80
Does anyone know how to fix this problem?
In response to Stefano Ortisi's comment:
I did not change anything else in .htaccess. I just kept the default settings.
RewriteBase /dev
<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]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

It is not an error that it redirects without the trailing slash.
In your public/.htaccess as you can see there is the following lines:
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
^^^ as the comment states that will redirect a url with a trailing slash if it is not a folder. If you remove that you will see it will not redirect you.

Related

Apache vhost double slash on redirects

I was trying to force the FQDN and HTTPS via Apache Vhost redirects in a Laravel application behind a reverse Proxy. My problem now is that the user sometimes gets redirected to a URL with double slash e.g. https://exampledomain.xy//test instead of https://exampledomain.xy/test and I'm not sure why.
Here is my apache vhost configuration:
<VirtualHost *:80>
ServerName localhost
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html/public
<Directory /var/www/html>
AllowOverride All
</Directory>
ErrorLog /dev/stderr
TransferLog /dev/stdout
RewriteEngine on
RewriteCond %{HTTP_HOST} !=localhost
RewriteCond %{HTTP_HOST} !=127.0.0.1
RewriteCond %{REMOTE_ADDR} !=127.0.0.1
RewriteCond %{REMOTE_ADDR} !=::1
RewriteRule (.*) https://exampledomain.xy/$1 [L,R=301]
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot "/var/www/html/public"
ServerName exampledomain.xy
</VirtualHost>
And my .htaccess file (default .htacces file that comes with Laravel)
<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]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
Okay looks like I fixed the issue. I changed the following in my config:
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://stk-support.ukl.uni-freiburg.de$1 [R=301,L]
The extra slash in the rewrite rule was causing the issue and I also added a HTTPS rewrite condition.

HTTPS on laravel5 not fully working

I have been trying to make HTTPS work on Laravel.
The routes do no longer work when the Laravel application goes on HTTPS.
Expected: the route loads the view.
Result: 404 Not found page.
Note: that this does work without https.
Here is my apache2 conf
<VirtualHost *:80>
ServerAdmin email#something
ServerName dev.something
DocumentRoot /var/www/something/development/public_html
DirectoryIndex index.php
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Options -Indexes
<Directory "/var/www/something/development/public_html/">
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
AuthUserFile /var/www/something/.htpasswd
AuthName "Password please"
AuthType Basic
Require valid-user
</Directory>
Here is my .htaccess
<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]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Force SSL
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
The following code forces laravel5 to go into secure mode.
app/Providers/AppServiceProvider.php
public function boot()
{
\URL::forceSchema('https');
}
Packages like Barryvdh\Debugbar could no longer load their assets.
I honestly no longer have a clue how I could do this.
If you know something about this please respond!

Fresh Laravel 5 shows 500 Internal Server Error no matter what (OS X, Apache)

My setup is OS X Yosemite, default Apache and PHP 5.6.
Vhost is set correctly, I have two other apps running ok:
<VirtualHost *:80>
ServerAdmin polar
ServerName lara5.com
ServerAlias lara5.com
DocumentRoot "/Users/polar/Sites/lara5/public"
ErrorLog "/private/var/log/apache2/dummy-host2.example.com-error_log"
CustomLog "/private/var/log/apache2/dummy-host2.example.com-access_log" common
</VirtualHost>
Hosts file is OK.
I've set the permissions to 777 to the whole project folder, still nothing.
If I remove .htaccess, the splash screen shows for the url lara5.com but the /home route returns 404.
I have tried both .htaccess (inside /public, ofc) Laravel provides, none of them work, 500 doesn't matter which route I access.
Default:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Other one from the docs:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Any ideas?
Thanks.
Translating comment into an answer to help out anyone else comes across this problem.
Based on your snippet from error.log it seems that Options directive isn't allowed in your .htaccess.
You can fix this error by commenting any line in your .htaccess starting from:
Options
To enable use of Options in .htaccess use:
Options All
in your Apace config's <Directory> section of your DocumentRoot.

Trailing slash in url and virtual host

I have a laravel project on localserver and want it to work even when the trailing slash is added to the url. It is working fine when accessed with localhost url. The htaccess is as follows:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteBase /LaravelProject/public/
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ $1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Trying to access page like
http://localhost/LaravelProject/public/login
and
http://localhost/LaravelProject/public/login/
is working fine.
However I have also VirtualHost defined like this
<VirtualHost *:80>
DocumentRoot "C:/wamp/www/LaravelProject/public"
ServerName LaravelProject.dev
ServerAlias www.LaravelProject.dev
ErrorLog "c:/wamp/logs/apache_error.log"
CustomLog "c:/wamp/logs/access.log" common
</VirtualHost>
Via url laravelproject.dev, I can access the document root and can see the "You have arrived." But when I try to access other url say laravelproject.dev/login, it gives me 500 Internal Server Error.
If I remove the RewriteBase from .htaccess and modify the RewriteRule as
RewriteRule ^(.*)/$ /$1 [L,R=301]
then the urls laravelproject.dev/login and laravelproject.dev/login/ work fine.
If I do this, again the problem persists with localhost url with trailing slashes and the
url http://localhost/LaravelProject/public/login/ redirects to http://localhost/login.
I want to be able to use both localhost and virtualhost urls with and without trailing slashes and give the same result. As it seems, it is one or the other for me.
I don't think you need any of these lines in your .htaccess file. I have got mine set up like this on a couple of projects and it works perfectly fine:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>

RewriteRule to cause subdomain to load another DocumentRoot?

Apache (mostly) noob here... could sure use a little help with what I am trying to do with a RewriteRule in .htaccess:
...to causes URLs using our sharpedge. subdomain to auto-load from the Internet_IE directory - one level deeper than the site root.
I have this in httpd.conf:
[snip]
NameVirtualHost 11.22.33.44
<VirtualHost 11.22.33.44>
Options All +ExecCGI
ServerAdmin hostmaster#ourhost.com
DocumentRoot /var/www/html/ourdomain.com
ServerName ourdomain.com
ServerAlias www.ourdomain.com
DirectoryIndex index.htm index.html index.dna
#---------------------------------
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
</IfModule>
#---------------------------------
</VirtualHost>
<VirtualHost 11.22.33.44>
Options All +ExecCGI
ServerAdmin hostmaster#ourhost.com
DocumentRoot /var/www/html/ourdomain.com
ServerName sharpedge.ourdomain.com
DirectoryIndex index.htm index.html index.dna
#---------------------------------
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
</IfModule>
#---------------------------------
</VirtualHost>
[snip]
...and this in .htaccess (in the site root, here: /var/www/html/ourdomain.com/)
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^sharpedge\.ourdomain\.com$
# years ago this next line was used here, but in httpd.conf: (I don't know what it was supposed to do)
# RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule ^sharpedge\.ourdomain\.com(.*) /var/www/html/ourdomain.com/Internet_IE/$1 [L]
</IfModule>
..but nothing happens from the RewriteRule; it is as if the RewriteRule was not there.
I'd much appreciate any suggestions.
Edits:
I would just change the DocumentRoot to the directory I actually want to serve, but I need to be able to call (include) files from within files in that directory with a prefixing / in the include path, where those files are located in the site root. Those files are also similarly called from other files that are in the site root.
You don't want to put any part of your host in the RewriteRule, but you got the HTTP_HOST right, try:
RewriteCond %{HTTP_HOST} ^sharpedge\.ourdomain\.com$ [NC]
# to prevent looping
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /Internet_IE/$1 [L]
Try changing the !-f and !-d checks:
RewriteCond %{HTTP_HOST} ^sharpedge\.ourdomain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/Internet_IE/
RewriteRule ^(.*)$ /Internet_IE/$1 [L]