zcart nginx configuration + htaccess conversion - laravel

Please help me to convert zcart application's htaccess rules to nginx configuration. Please see below htaccess.
<IfModule mod_rewrite.c>
RewriteEngine On
# 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]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
Thanks

Assuming you are already configure your FastCGI part of config, lets try:
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
can be converted to nginx configuration as
location ~ ^(.+)/$ {
if (!-d $request_filename) {
return 301 $1;
}
# else this is a directory, will be processed via index file in this directory by default
}
or (the same result)
location ~ ^(.+)/$ {
if (!-d $request_filename) {
rewrite ^(.*)/$ $1 permanent;
}
# else this is a directory, will be processed via index file in this directory by default
}
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
will be processed by a single try_files directive in the main location block:
location / {
try_files $uri $uri/ /index.php;
}
And, finally,
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
requires no additional config in nginx.conf, because $_SERVER[HTTP_AUTHORIZATION] variable will be appended to $_SERVER array automatically when HTTP Authorization header is set (if your php-fpm configuration correct).

Related

HTTPS redirects on Heroku Laravel not working correctly

I tried to force HTTPS on subdomain Heroku/Laravel. See .htaccess below.
It didn't work. Instead the subdomain "https://sub.domain.com/anything" redirect to "https://sub.sub.domain.com/index.php". See the double "sub" and the "index.php" in the URL?
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]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# If we receive a forwarded http request from a proxy...
RewriteCond %{HTTP:X-Forwarded-Proto} =http [OR]
# ...or just a plain old http request directly from the client
RewriteCond %{HTTP:X-Forwarded-Proto} =""
RewriteCond %{HTTPS} !=on
# Redirect to https version
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Could you please try following htaccess file. Based on your shown samples only, have changed the sequence of your Rules(couldn't test it). Please make sure you clear your browser cache before testing any URLs.
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# If we receive a forwarded http request from a proxy...
RewriteCond %{HTTP:X-Forwarded-Proto} =http [OR]
# ...or just a plain old http request directly from the client
RewriteCond %{HTTP:X-Forwarded-Proto} =""
RewriteCond %{HTTPS} !=on
# Redirect to https version
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
An alternative and better solution is to force HTTPS in Laravel directly and not in NGINX.
This way Laravel generates directly HTTPS URLs, and you don't have to redirect it.
To force HTTPS in Laravel you should add the following inside App\Providers\AppServiceProvider:
/**
* Bootstrap any application services.
*/
public function boot()
{
if (!App::environment([
'local',
'testing',
])) {
URL::forceScheme('https');
}
}

non www url is redirecting to homepage

I have .htaccess settings to get any non-www request to www.
It works for the home page, but when I check other URLs it goes to the home page.
Here are my settings (UPDATED): Top part is coming from laravel
<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>
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
#First rewrite any request to the wrong domain to use the correct one (here www.)
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
#Now, rewrite to HTTPS:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* ? [F,L]
</IfModule>
For example
https://example.com goes to https://www.example.com.
But https://example.com/contact doesn't go to https://www.example.com/contact. Instead, it goes to https://www.example.com.
What can be the problem and solution?
but https://example.com/contact doesn't go to https://www.example.com/contact
Assuming you are not simply using extensionless URLs (ie. /contact maps to /contact.php) with MultiViews/mod_negotiation then you must have other mod_rewrite directives that route these requests.
It looks like you may have put these directives in the wrong order.
The external redirects in your question need to go near the top of your .htaccess before any internal rewrites to a front-controller.
Aside:
#First rewrite any request to the wrong domain to use the correct one (here www.)
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
This rule should be redirecting to https://, not http://. Otherwise you are going to get multiple, unnecessary redirects (https to http and back to https).
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* ? [F,L]
This rule (currently last), needs to go at the top. Generally, any blocking directives should be first. You can also simplify/optimise this a bit...
RewriteCond %{HTTP_USER_AGENT} libwww-perl
RewriteRule ^ - [F]
L is implied when using F. If there is no substitution string then you should use -, not ?.
UPDATE: Following your updated question, my initial answer/hypothesis is confirmed... your directives are in the wrong order. Your external redirects need to go before the Laravel front-controller.
Otherwise, in its current order, a request of the form example.com/contact is first internally rewritten to index.php by the Laravel front-controller and then there will be an external redirect to the canonical scheme + hostname, so the net result is you are redirected to the homepage.
The cPanel redirect you mentioned in comments simply manifests itself as a rule in .htaccess (which you've already included). But note that all cPanel redirects are placed (often incorrectly) at the end of the .htaccess file (this is stated in the cPanel docs).
So, in summary, your directives should be written like this:
Options -MultiViews -Indexes
RewriteEngine on
#
# BLOCKING DIRECTIVES
#
# Block certain requests
RewriteCond %{HTTP_USER_AGENT} libwww-perl
RewriteRule ^ - [F]
#
# CANONICAL REDIRECTS
#
# Redirect non-www to www + HTTPS
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [L,R=301]
# Redirect HTTP to HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Remove index.php
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
#
# Laravel directives follow...
#
# 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]
ıt seems that is working with this
<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}]
## Force https and www
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
# 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]
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* ? [F,L]
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
</IfModule>
Any extra suggestions to optimize are accepted with pleasure

Rewrite rule not working, return 404 not found

i configure the htaccess as shown below
// this htaccess in /var/www/html
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ blog1/public/ [L]
RewriteRule ^market(.*)$ blog2/laravel/ [L]
</IfModule>
this is the htacess in blog2
Options -MultiViews
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ / [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}]
I moved my index.php from laravel folder to blog2.
when i access http://website.com/market it will always go to the blog1 rule, not redirect to blog2/laravel.
there will be fine if http://website.com/blog2, but the reason i want to do this because want to make the url in this way http://website.com/market-us etc.
What's wrong with my configuration?
Try switching places for your two rewrite rules. Put ^market(.*)$ as the first one, and ^(.*)$ blog1/public/ [L] right under it.

Laravel redirect all http to https

I have a website in Laravel which i switched over to https a while ago. I have succeeded to redirect all secure non-www (https://) URL's to www (https://www.) variants with the help of other posts, but am getting stuck on getting both non-secure variants (http://) and (http://www.) redirected to the secure www variants (https://www.)
When you call for the non-secure, www version:
http://www.apk-vervaldatum.nl,
It redirects to:
https://www.apk-vervaldatum.nl/public/https://www.apk-vervaldatum.nl
When you call for the non-secure, non-www version:
http://apk-vervaldatum.nl,
It first redirects to:
https://www.apk-vervaldatum.nl
Then redirects to:
https://www.apk-vervaldatum.nl/public/https://www.apk-vervaldatum.nl
My htaccess in the root folder looks is:
*Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ public/$1 [L]*
My htacces in public folder looks is:
*Options -MultiViews -Indexes
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]*
Here you have method how to enforce using HTTPS
Go to AppServiceProvider:
namespace App\Providers;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
URL::forceScheme('https'); // add this
}
}
Another approach is change is .htaccess:
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Good luck!
your .htaccess should be :
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Start with www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# 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>

Htaccess Laravel - Removing Public Folder URL

I already upload my project to my host
The link is examplesite.com/Hiflyer/public/index
I want all my route their look like this.
examplesite.com/index
examplesite.com/news
This is my HTACCESS:
<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>
The question is how to configure it?
Perform 2 simple steps and you could run your laravel project without /public folder.
1.) Move the .htaccess file to the main directory from public folder without any changes.
2.) And do the same with index.php.
Rest .htaccess file working has following content.
RewriteEngine On
#----------------------------------------------
# | this code use for remove public directory |
#----------------------------------------------
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{REQUEST_URI} !(\.ico|\.css|\.js|\.png|\.jpg|\.gif|robots\.txt|\.eot|\.svg|\.ttf|\.woff|\.woff2|\.otf|\.pdf)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(login|uploads|assets|css|js|images|ca|favicons|fonts|)/(.*)$ public/$1/$2 [L,NC]
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
And index.php
<?php
/**
* Laravel - A PHP Framework For Web Artisans
*
* #package Laravel
* #author Taylor Otwell <taylor#laravel.com>
*/
$uri = urldecode(
parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
);
// This file allows us to emulate Apache's "mod_rewrite" functionality from the
// built-in PHP web server. This provides a convenient way to test a Laravel
// application without having installed a "real" web server software here.
if ($uri !== '/' && file_exists(__DIR__.'/public'.$uri)) {
return false;
}
require_once __DIR__.'/public/index.php';

Resources