How always redirect at https://www.? - .htaccess

How to make that my laravel 5.8 site always works with https://www. prefix ?
In .htaccess i wrote
<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}]
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://www.%{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]
RedirectMatch 404 /\.git
# RewriteRule ^$ info.php [QSA]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
But looks like RewriteRule is not applicable is some case when I run url like “mysite.com” browser redirects “https://mysitede.com”.
Also to be sure that I work https in my app/Providers/AppServiceProvider.php I have :
class AppServiceProvider extends ServiceProvider
{
use funcsTrait;
public function boot()
{
if( $this->isHttpsProtocol() ) { // check I am not on local server
\URL::forceScheme('https');
}
Which is the decision ?

The following .htaccess code redirects requests to the https and www versions of your web pages. Add to your site's root .htaccess file:
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteRule (.*) https://www.%1/$1 [R=301,L]

Related

Removing only one of the 2 public values in the URL with htaccess

I have an e-commerce system written in Laravel 8. Public directory appears in URL.
The software's URL system is as in the network.
Homepage: website.com/public/Category: website.com/public/category
Product: website.com/public/category
CSS file URL address: website.com/**public**/themes/themename/**public**/css/style.css
JS File URL address: website.com/**public**/themes/themename/**public**/js/app.js
As you can see, there are 2 public values in CSS and JS files.
I updated the htaccess file as follows to remove the public value.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.website\.com
RewriteRule (.*) https://www.website.com/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#remove public/ from URLs using a redirect rule
RewriteCond %{THE_REQUEST} \s/+(.+/)?public/(\S*) [NC]
#RewriteRule ^ /%1%2? [R=301,L,NE]
#Remove index.php
#RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
#RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
#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}]
</IfModule>
However, it also removed the 2nd public value of CSS and JS files. That's why CSS and JS files fail.
How can I keep the 2nd public value constant and remove the first public value?
Have your htaccess file in following way, please make sure to clear your browser cache before testing your URLs. I have also corrected few flags in your other rules too.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.website.com%{REQUEST_URI} [NE,R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
# remove public/ from URLs using a redirect rule
RewriteCond %{THE_REQUEST} \s/(themes/themename)/public(/css/app\.css|/js/app\.js)\s [NC]
RewriteRule ^ %1%2? [R=301,L]
RewriteRule ^(themes/themename)/(css/app\.css|/js/app\.js)/?$ $1/public/$2 [NC,L]
# 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}]

Rewritecond by `%{HTTP_HOST}` is ignored?

I have multiple domains that point to the same folder on my server. The code in index.php page recognizes which domain is accessing it and shows different content for each domain.
Now I want that www.domain-a.com\sitemap.xml opens /sitemap-a.xml so that each domain has its own sitemap.
I use the following rule in my .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^.*domain-a\.com$
RewriteRule ^sitemap.xml$ sitemap-a.xml [NC]
However, if I access www.domain-a.com\sitemap.xml then the content from the file /sitemap.xml is shown instead of the file sitemap-a.xml.
Is the redirecting rule wrong? If so, how can I fix it?
I am using Laravel. This is the full .htaccess file:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^.*domain-a\.com$
RewriteRule ^sitemap.xml$ sitemap-a.xml [NC]
# browser request: .php to non-php
# https://stackoverflow.com/questions/13222850/redirect-php-urls-to-urls-without-extension
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
# [NC] is a case-insensitive match
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# 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>
Can you try this rule just above # Handle Authorization Header line:
RewriteCond %{HTTP_HOST} (?:^|\.)domain-a\.com$ [NC]
RewriteRule ^sitemap\.xml$ /sitemap-a.xml [L,NC]

Laravel: RewriteRule in .htaccess does not work

In my Laravel app I added to the /public/.htaccess/ file a RewriteRule like this:
RewriteRule ^resources/pdf/?$ /about
however, if I call www.mydomain.com/resources/pdf/ the I only get this error:
although my server uses APACHE and the other htaccess entries do work. Also www.mydomain.com/about does work.
Why is this not working? Here is the full htaccess file:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteRule ^resources/pdf/?$ /about [L]
RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
# [NC] is a case-insensitive match
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# 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>
Try with:
RewriteRule ^resources/pdf/?$ about [NC]
Which works with www.mydomain.com/resources/pdf/ and www.mydomain.com/resources/pdf
I was missing the [L]:
RewriteRule ^resources/pdf/?$ /about [L]
Also I had to clear cache and view the page in incognito mode to witness the effect.

.htaccess redirect to HTTPS doesn't work correctly

Redirect works only on the first page, http://example.com to https://example.com.
But it doesn't work on subfolders: http://example.com/news or /news/category etc. redirects me to the https://example.com/index.php (My website is on Laravel 5.5).
Any suggestions pls?
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# ensure https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
# ensure !www.
RewriteCond %{HTTP_HOST} ^www\.(.*)$
RewriteRule ^(.*)$ https://%1/$1 [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}]
Header add Access-Control-Allow-Origin "http://glyphicons.com/"
</IfModule>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
Remember to replace example.com and www.example.com with your actual domain name

htaccess get https for a folder excluded from rewrites rules

I'm using Laravel, so I have rewrite rules to redirect to the index.php, I've rules to redirect to www and to https too.
I want URI "/blog" not to be redirected to index.php but I need it like:
https://www.example.com/blog
actually it redirects to:
example.com/blog
becouse this rule RewriteCond $1 !^(blog) exludes it form rewrite rules
This are my rules:
<IfModule mod_rewrite.c>
RewriteBase /
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteCond $1 !^(blog) #I need this to get www and https but not further rules
# To https
RewriteCond %{REQUEST_SCHEME} !https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# To www if is not a subdomain
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} !^(.*)\.(.*)\. [NC]
RewriteRule .* https://www.%{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]
If you want blog/ to be skipped from index.php rewrite then change last rule to this:
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^blog(/.*)?$ index.php [L,NC]

Resources