Pretty urls not working laravel 5.2 on a https protocol - apache

Pretty urls was working on when it was on "http" using this tutorial (https://laravel.io/forum/09-15-2015-removing-indexphp-from-url-laravel-5116) i figured it out but as i configured the "https" it stopped working
(Not Found
The requested URL /login was not found on this server.
Apache/2.4.7 (Ubuntu) Server at wasamar.com.ng Port 443), what did i do wrong?
by the way i am using an ubuntu cloud server 14.04
This is my virtual host config /etc/apache2/sites-avalable/wasamar.com.ng.conf
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
ServerName wasamar.com.ng
ServerAlias www.wasamar.com.ng
ServerAdmin info#wasamar.com.ng
DocumentRoot /var/www/html/wasamar/public/
# Redirect permanent "/" "https://wasamar.com.ng/"
<Directory /var/www/html/wasamar/public/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For examplethe
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin info#wasamar.com.ng
ServerName wasamar.com.ng
DocumentRoot /var/www/html/wasamar/public/
# SSL Engine Switch:
# Enable/Disable SSL for this virtual host.
SSLEngine on
<Directory /var/www/html/wasamar/public/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
# A self-signed (snakeoil) certificate can be created by installing
# the ssl-cert package. See
# /usr/share/doc/apache2.2-common/README.Debian.gz for more info.
# If both key and certificate are stored in the same file, only the
# SSLCertificateFile directive is needed.
SSLCertificateFile /etc/apache2/ssl/wasamar.com.ng/apache.crt
SSLCertificateKeyFile /etc/apache2/ssl/wasamar.com.ng/apache.key
</VirtualHost>
</IfModule>
This is my .htaccess file
<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]

I think the https redirect should be first:
RewriteEngine On
# Force SSL
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
...

Related

Letsencrypt apache, disable https for specific directory

Im using ubuntu, apache and letsencrypt. I want to disable https for one directory.
This is my .htaccess right now:
RewriteEngine On
RewriteBase /
#HTTP TO HTTPS
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
domain.nl should redirect to https://domain.nl. (As it does now)
http://domain.nl/keuken (or http://domain.nl/keuken/keuken.php is also ok) should just be http.
I can not get it to work.
I tried several solutions from stackoverflow but either it does nothing or i get to many redirects.
I found the answer. The .htaccess in my question has nothing to do with it.
I changed the virtualhost config file: 000-default.conf in /etc/apache2/sites-available to this:
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port t$
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
<Directory /var/www/html>
AllowOverride All
</Directory>
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/keuken(/|$) [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
If i am correct this means everything except /keuken is redirected to https.

Rewrite mode doesn't work with virtualhost and symfony4

I'm using symfony4.3 and the application is hosted under vps ubuntu 18.04.
I have followed https://symfony.com/doc/current/setup/web_server_configuration.html to configure the web server but the problem is that the application doesn't work with rewrite mode, but it works when adding /public/index.
I mean www.mysite.com doesn't work. It show the folders and files but www.mysite.com/public/index.php dispaly the website content.
The domain name is secured with https
This is the configuration:
<VirtualHost *:80>
ServerAdmin webmaster#s****.com
ServerName www.s****.com
ServerAlias s****.com
# For Apache 2.4.9 or higher
# Using SetHandler avoids issues with using ProxyPassMatch in combination
# with mod_rewrite or mod_autoindex
<FilesMatch \.php$>
SetHandler proxy:fcgi://127.0.0.1:9000
# for Unix sockets, Apache 2.4.10 or higher
# SetHandler proxy:unix:/path/to/fpm.sock|fcgi://dummy
</FilesMatch>
DocumentRoot /var/www/site/public
DirectoryIndex /index.php
<Directory /var/www/site/public>
AllowOverride All
Require all granted
FallbackResource /index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
# Sets the HTTP_AUTHORIZATION header removed by Apache
RewriteCond %{HTTP:Authorization} .
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect to URI without front controller to prevent duplicate content
# - use Apache >= 2.3.9 and replace all L flags by END flags and remove the
# following RewriteCond (best solution)
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]
# If the requested filename exists, simply serve it.
# We only want to let Apache serve files and not directories.
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# Rewrite all other queries to the front controller.
RewriteRule ^ %{ENV:BASE}/index.php [L]
</IfModule>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
# When mod_rewrite is not available, we instruct a temporary redirect of
# the start page to the front controller explicitly so that the website
# and the generated links can still be used.
RedirectMatch 307 ^/$ /index.php/
# RedirectTemp cannot be used instead
</IfModule>
</IfModule>
</Directory>
# optionally disable the fallback resource for the asset directories
# which will allow Apache to return a 404 error when files are
# not found instead of passing the request to Symfony
<Directory /var/www/site/public/bundles>
FallbackResource disabled
</Directory>
# uncomment the following lines if you install assets as symlinks
# or run into problems when compiling LESS/Sass/CoffeeScript assets
<Directory /var/www/site>
Options FollowSymlinks
</Directory>
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
# optionally set the value of the environment variables used in the application
#SetEnv APP_ENV prod
#SetEnv APP_SECRET <>
#SetEnv DATABASE_URL "mysql://db_user:db_pass#host:3306/db_name"
</VirtualHost>
Solved:
I have another file default-ssl.conf , I must put those configuration inside:
<IfModule mod_ssl.c>
<VirtualHost _default_:443>
ServerAdmin contact#s****.com
ServerName www.s****.com
ServerAlias s****.com
DocumentRoot /var/www/site/public
<Directory /var/www/site/public>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Allow from All
Require all granted
<IfModule mod_rewrite.c>
# copy configuration here
</VirtualHost>
</IfModule>

forcing Codeigniter HMVC to use SSL

I have a website build with Codeigniter HMVC. On my local machine using Wamp it runs like it should. I want to upload it to my webserver that has an SSL certificate.
No matter what i try, i cant get the website to run on my webserver. I have tried following this :
How to force ssl in codeigniter?
But none of the given solutions work in my situation.
my .htaccess file looks like this :
Options +FollowSymLinks
Options -Indexes
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
in config.php of codeigniter i have :
$config['base_url'] = 'https://www.example.com/';
my webserver is an ubuntu 16 machine with apache, using an letsencrypt certificate. mod rewrite is enabled, but besides that is a standard configuration.
I have tried redirecting with hooks as mentioned in the link above, but to no avail. I have also tried various .htaccess file configuration but also no result there.
Is there anyone who has this running on their server?
regards,
sander
Because you say,
my webserver is an ubuntu 16 machine
I assume you are not using a shared server from a hosting provider and so you have complete access to Apache's configuration files. If that is true then you should not use .htaccess files. Read THIS and THIS to learn why.
I also assume you have a set of Apache "sites-available" files that define <VirtualHost> configs. It is inside those <VirtualHost> blocks where you should be placing the code you currently have in .htaccess.
To redirect all http requests to https try this as the contents of the "default" site in a "sites-available" configuration file e.g. "000-default.conf"
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
RedirectMatch 301 (.*) https://www.example.com$1
</VirtualHost>
What this will do is take any http request and immediately redirect to the https: URL. If you are using some other port besides the typical :80, adjust accordingly.
As mentioned, you can do the rewriting in a VirtualHost. Here's an example for the https: config file (maybe named along the lines of "020-example-443.conf")
<VirtualHost *:443>
ServerName www.example.com
ServerAlias example.com
DocumentRoot /var/www/whatever
ServerAdmin web-boss#example.com
<Directory /var/www/whatever>
DirectoryIndex index.php
Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [PT]
</Directory>
# SSL Engine Switch:
# Enable/Disable SSL for this virtual host.
SSLEngine on
# Certificates delivered by certbot - your's maybe elsewhere
SSLCertificateFile /etc/letsencrypt/live/yoursite/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/yoursite/privkey.pem
#SSLOptions +FakeBasicAuth +ExportCertData +StrictRequire
<FilesMatch "\.(cgi|shtml|phtml|php)$">
SSLOptions +StdEnvVars
</FilesMatch>
<Directory /usr/lib/cgi-bin>
SSLOptions +StdEnvVars
</Directory>
</VirtualHost>
With these in place, you won't have to do anything special in CodeIgniter except, as you already do, use the following
$config['base_url'] = 'https://www.example.com/';
You must add after RewriteEngine on
<IfModule mod_ssl.c>
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

mod_rewrite causing extra slash

I'm trying to simply remove .php from the filenames on URLs. I'm implementing the solution from this StackOverflow answer so my VirtualHost looks like this,
# domain: example.com
# public: /var/www/html/example.com/public_html/
<Directory /var/www/html/example.com/public_html>
Options +FollowSymLinks
AllowOverride All
DirectoryIndex index.html index.htm index.php
</Directory>
<VirtualHost *:80>
ServerName example.com
Redirect / https://example.com/
</VirtualHost>
<VirtualHost *:443>
# Admin details
ServerAdmin vanguard#example.com
ServerName example.com
ServerAlias www.example.com
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
#Index File
DirectoryIndex index.html index.php
DocumentRoot /var/www/html/example.com/public_html
#Log Details
LogLevel warn
ErrorLog /var/www/html/example.com/log/error.log
CustomLog /var/www/html/example.com/log/access.log combined
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L]
</VirtualHost>
Going to https://example.com resolves just fine, but if I try to go to https://example.com/examplefile it rewrites it to https://example.com//examplefile/
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L]
In a virtual host context this RewriteCond directive will never match because at the time the directive is processed the request has not yet been mapped to the file system, so REQUEST_FILENAME simply contains the URL-path (as opposed to the absolute filesystem path), which is the same as REQUEST_URI.
(This wouldn't work in .htaccess either, but for a different reason. In .htaccess you can't rewrite to an absolute filesystem path, so whilst the RewriteCond should match, the RewriteRule is likely to trigger a 403 Forbidden error.)
To fix this in a server/virtual host context, you can modify these directives to use a URL-based look-ahead:
RewriteCond %{LA-U:REQUEST_FILENAME}.php -f
RewriteRule !\.php$ %{LA-U:REQUEST_FILENAME}.php [L]
However, neither your original directives nor this correction explains the double slash you are seeing in your example. Either an erroneously cached response or your web application?

laravel the requested url was not found on this server

I've an Ubuntu 14.04 kernel. I was installing my Laravel application in this server.
After installing, I tried to set the root directory to public.
sudo nano /etc/apache2/sites-available/000-default.conf
I have only these options in the file
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port t$
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html/public/
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
I've changed the Document root to
DocumentRoot /var/www/html/public/
Now when I tried to access my Laravel App, through 123.xxx.xxx.xxx/ it shows the home page and working well. It gets all the GET variable too. Eg: 123.xxx.xxx.xxx?type=wefwef
But when I go to other links such as 123.xxx.xxx.xxx/login it gives me an error
Not Found
The requested URL /login/ was not found on this server.
Apache/2.4.7 (Ubuntu) Server at 104.236.234.85 Port 80
I have the routes.php working well on localhost. But not on this server. Please help me.
This looks like you have to enable .htaccess by adding this to your vhost:
<Directory /var/www/html/public/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
If that doesn't work, make sure you have mod_rewrite enabled.
Don't forget to restart apache after making the changes! (service apache2 restart)
I resolved by doing the following:
Check if there is a module called rewrite.load in your apache at:
cd /etc/apache2/mods-enabled/
If it does not exist execute the following excerpt:
sudo a2enmod rewrite
Otherwise, change the Apache configuration file to consolidate use of the "friendly URL".
sudo nano /etc/apache2/apache2.conf
Find the following code inside the editor:
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Change to:
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
After that restart the Apache server via:
sudo /etc/init.d/apache2 restart
First enable a2enmod rewrite
next restart the apache
/etc/init.d/apache2 restart
click here for answer these question
Alternatively you could replace all the contents in your .htaccess file
Options +FollowSymLinks -Indexes
RewriteEngine On
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
See the docs here.
https://laravel.com/docs/5.8/installation#web-server-configuration
For Ubunutu 18.04 inside a vagrant box ... This is what helped me
Ensure www-data has permissions to the .htaccess file
sudo chown www-data.www-data .htaccess
edit the apache2 conf to allow for symlinks etc
sudo nano /etc/apache2/apache2.conf
Add this to the file
<Directory /var/www/html/>
Options Indexes FollowSymLinks
AllowOverride all
Require all granted
</Directory>
Restart your apache2 server
sudo service apache2 restart
I hope this helps someone.
In addition to all the answers if you still encounter some variation of the problem, edit the .env file and set APP_URL to your domain name as in:
APP_URL=similar_to_my_avatar_link
In httpd.conf file you need to remove #
#LoadModule rewrite_module modules/mod_rewrite.so
after removing # line will look like this:
LoadModule rewrite_module modules/mod_rewrite.so
And Apache restart
too late.. but for the benefit
you can edit your .htaccess file
comment this line
# RewriteRule ^ index.php [L]
Make sure you have mod_rewrite enabled.
restart apache
and clear cookies of your browser for read again at .htaccess
I have faced the same problem in cPanel and I fixed my problem to add in .htaccess file below these line
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Add the following to .htaccess in public folder.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
In current version of Apache in httpd.conf there are lines:
DocumentRoot "${SRVROOT}/htdocs"
<Directory "${SRVROOT}/htdocs">
...
</Directory>
instead of earlier here mentioned:
DocumentRoot /var/www/
<Directory /var/www/
...
</Directory>
It depends on your server's directory structure, but by default I guess it can look like this.
As was recommended, inside Directory tag I changed options like this:
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
by default it was AllowOverride none. It helped.
And of course LoadModule rewrite_module modules/mod_rewrite.so in the same file should be switched on.