Stange with htaccess on Laravel 5.4 - .htaccess

I have site on laravel 5.4 with API routes ( prefix /API/v1/ ...). Now it's work fine. When I add some futures (Fo site routes, not API):
redirect from www to non www prefix
redirect from http to https
for API routes any types of request (with www or not, with http or https)
When I add redirects for this, it's work fine, but API have error (redirect to main site page).
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/api/.*$ [NC] # no for api
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Redirect www to non-www
RewriteCond %{HTTP_HOST} ^www.example.ru$ [NC]
# RewriteCond %{REQUEST_URI} !^/api/.*$ [NC] # no for api
RewriteRule ^(.*)$ http://example.ru/$1 [R=301,L]
# Redirect http to https.
RewriteCond %{HTTPS} !=on
# RewriteCond %{REQUEST_URI} !^/api/.*$ [NC] # no for api
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,QSA]
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
But when I test API routes it's redirect to main page example.ru/. For example:
I call POST to http://www.example.ru/api/v1/login, I return the main page html code.
Where I do mistake. Please help. Best regards.

Related

How To Redirect www to Non-www with path url complete in apache2

All paths with www (www.example.com/posts/categories) redirect to the home page. How can i redirect from .htaccess and keep the full path?
https://www.example.com/posts/categories to https://example.com/posts/categories
current configuration:
<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]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
</IfModule>
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
You've put this rule in the wrong place. It needs to go at the start, immediately after the RewriteEngine directive. By placing it at the end, after the rewrite to index.php, any request for the www subdomain will be redirected to index.php (during the second round of processing).
As a general rule, external redirects should always go before internal rewrites.
You should also consider implementing an HTTP to HTTPS redirect.

htaccess www well in home page but not in other pages

My .htaccess file content
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]
# Remove index.php
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^ /%1 [R=301,L]
# Redirect Http To Https
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Redirect Non Www To Www
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*) https://www.%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
my example.com redirected well to www.example.com (Not problem)
my problem when I go to other pages like
example.com/page1/test redirected to www.example.com
(That my problem) I need it redirect me to www.example.com/page1/test
Your directives are in the wrong order. You need to put your canonical redirects before the front-controller - at the top of the file.
By placing the redirects at the end they are simply never going to get processed for anything other than static files/directories since everything else is routed to index.php.
Your HTTP to HTTPS redirect is also incorrect since you are unconditionally prefixing with the www subdomain.
Also, no need to repeat RewriteEngine throughout the file.
In other words, arrange your directives like this (assuming you are not intending to implement HSTS):
Options +FollowSymLinks -Indexes
RewriteEngine On
# Remove index.php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^ /%1 [R=301,L]
# Redirect Non Www To Www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
# Redirect Http To Https
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP:Authorization} .
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
It is more efficient to use a regex like ^ to simply be successful for every URL, rather than .* that needs to traverse the entire URL-path - since you are not capturing this.
You will need to clear your browser cache before testing. Preferably test with 302 (temporary) redirects to avoid caching issues.

.htaccess not forwarding requests to another domain

This is my .htaccess
RewriteEngine on
# If a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward it to index.php
RewriteRule . index.php
RewriteCond %{HTTP_HOST} ^api.example.com [NC]
RewriteRule ^(.*)$ https://www.example.com [L,R=301,NC]
#For api send authorization header
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
When I try to post to
https://api.example.com/user/login
it gives me this
Shouldn't it just forward my request to https://www.example.com
I can't figure it out why it won't work.
EDIT:
The problem might be that ssl for https://api.example.com doesn't work

How to remove ssl from specific url or page with htaccess

I am having a site with ssl. All pages from site are redirecting to https with www.
I want following urls to be non ssl, so it will work with http only.
www.domain.com/admin/order/remote_request?
www.domain.com/admin/order/print/131756
My current htacess file is ad follows
<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]
# Redirect non www request to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# Redirect http request to https
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Try :
# Redirect http request to https
RewriteCond %{THE_REQUEST} !/admin/order/remote_request\? [NC]
RewriteCond %{THE_REQUEST} !/admin/order/print/131756 [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

htaccess -> need to redirect all www to non-www and one page only to ssl

I've looked "everywhere" but just can't seem to figure this one out.
I need the htaccess to do 2 things for me:
Force all www requests to non-www requests
Also force http (non-ssl) on all pages but one in which case I need ssl
Also, this is a wordpress site using permalinks, so I've got that chunk of htaccess code wordpress also puts in there. But somehow everything works fine, except for when I go to the page that needs to be forced to ssl, then it just redirects to the root of the site.
This is what my code looks like:
RewriteEngine On
RewriteBase /
# Redirect from www to non-www
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301]
# Turn SSL on for secure-page
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^secure-page[/]?$
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
# Turn SSL off everything but secure-page
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^secure-page[/]?$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
# BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
So results are:
any www and non-www and http urls are redirected to non-www and http -> OK
any www and non-www and https urls (excl. secure-page) are redirected to non-www and http -> OK
secure-page url are redirected to the site root and I can't figure out why -> NOT OK
Can someone please help?
Thanks
RewriteEngine On
# Redirect from www to non-www
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/secure-page[/]?$
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/secure-page[/]?$
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
# Turn SSL on for secure-page
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/secure-page[/]?$
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
# Turn SSL off everything but secure-page
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/secure-page[/]?$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
I've updated my answer!

Resources