Refresh page from browser returns 404 - .htaccess

I am using Laravel for my application. Sorry in advance if this is a known issue. My system works well until I refresh the browser. It happens only when I refresh from the main page which not from other pages of the site. It return 404
Maybe I missed one important thing. It does not happen on my local but only on my VPS
Following is my htaccess file
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
# 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>
Inspecting the network from the browser returns cached until the initiator is 404 where I see a duplication on of the path to my site root
Instead of "mysite.xx/path/mainfolder" it contains "myste.xx/path/mainfolder/path/mainfolder"

Related

Add HTTPS redirect for Progressive Web App with HTML 5 history mode

I have a progressive web application, built using Vue CLI 3, that is currently using the HTML 5 history mode. I have followed the documentation to add the necessary configuration to the .htaccess, however this example does not cover redirecting to HTTPS. This is what I have:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
I have followed other Stack Overflow responses on how to redirect all requests to HTTPS via .htaccess, but this results in an endless redirect loop. So what I need is to redirect non HTTPS requests to HTTPS while still honoring the index.html redirect.
After additional research, I was able to fix my problem by prepending the .htaccess file with some HTTPS specific configuration. So now my file looks like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !=https
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,N]
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>

redirect htaccess for facebook crawler

I've created a SPA app with vuejs. Everything was perfect until I want to share dynamic content on Facebook.
After research I found that I need another file (In my case php file) when I fill the meta-tags for facebook crawler.
In htaccess I'm trying to detect to Facebook agents and to redirect to specific file to fill meta-tags.
This is my .htaccess:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.html [NC,QSA]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Crawler
RewriteCond %{HTTP_USER_AGENT} (facebookexternalhit/[0-9]|Facebot|Twitterbot|Pinterest|Google.*snippet)
RewriteRule cars/carDetail(.*)$ http://website.api.com/v1.0/meta-tags$1 [R=301,L]
But is not working. When you try to debug from https://developers.facebook.com/tools/debug/sharing/
url:
http://website.com/cars/carDetail?id=48
Is not redirecting to:
http://website.api.com/v1.0/meta-tags$1
Thanks
I solved my problem.
The redirect condition should be on the top of htaccess. Also I've add "RewriteBase /" option on the top:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# allow social media crawlers to work by redirecting them to a server-rendered static version on the page
RewriteCond %{HTTP_USER_AGENT} (facebookexternalhit/[0-9]|Twitterbot|Pinterest|Google.*snippet)
RewriteRule cars/carDetail(.*)$ http://website.api.com/v1.0/meta-tags$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.html [NC,QSA]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

How to avoid "too many redirects" error in .htaccess?

I have the following code in my .htaccess file.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
#RewriteCond %{HTTPS} off
#RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteBase /
# 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>
I want to redirect the domain from http to https. But when I try to do that I am getting the error "too many redirects" error.
We recommend putting a simple Redirect statement in your main config file, in the http vhost, rather than using .htaccess files for this. There are nunerous reasons for this which are covered in more detail in the httpd FAQ.
Redirect / https://example.com/
We also recommend using FallbackResource for the front controller stuff
FallbackResource /index.php

mod_rewrite and RewriteRule blockade me from add error page

i have this .htaccess file, but i need to want to have a 404 error page. but when i add ErrorDocument to it i do nothing. if i write localhost:8888/home/somepage-there-not-exist.
<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>
it's located in the root same with the app and public folder
Ind the public folder there are this too:
<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>
I know the code coveret /something to files request, but when the file not exit it should get a 404 error.
It's from laravel's root .htaccess after some i like some part of the framework, but i wanna make a code block there only is there to make the website secure, and not a framework (don't wanna invent the wheel again). But i still need it to understand urls that way because it make it SEO and open so there no rules are for the URL
So i have student laravel's code and figure out how it works, but this part about 404, 403 and so on.. is so complicated in laravel's and cross so maney files that you don't have a chance to understand the code and find out how laravel know when it's a 404 error.
UPDATE
I have found this ErrorDocument-VS-RewriteRule
after what i see i can use
RewriteRule ^(.*)$ - [L,R=404]
ErrorDocument 404 /errors/errors.php
thats kinda works, but not 100%. the ErrorDocument don't work when you are 2 steps in example: localhost/step1/step2 and it also broke it so when the site exist it still show the error page. i have try to set the code over and under the RewriteRule ^(.*)/$ /$1 [L,R=301]
Yes this project is maybe stupid but i do it for lerning

Laravel throwing 404 on images/css/js

Laravel is throwing a 404 error on my images, css and javascript files which are located in /mysite/public. I've set /mysite/public as the site's root folder and all my assets are located in there.
I did a google search on this error but they all gave the same solution i.e {{asset('css/style.css')}}. I already have my links setup like this so I don't think this is the problem.
I think the error has something to do with my Rewrite rules but I just can't figure our what it is.
My site's .htaccess file is:
RewriteEngine On
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.my-site.com/$1 [R,L]
</IfModule>
If I delete the first RewriteRule, my css loads fine but pages other that the homepage will start throwing a 404 error. If I put it back, other page work fine but css, js and image file stop loadings.
Your first rule essentially says "regardless of what the URL is, load index.php". This means it will never load your assets, which are physical folders, as it will just load index.php instead. For example, if you tried to load css/image.png it will just load index.php.
If you remove it, your actual folders will load, but other URLs aren't re-written to index.php, which is why it will break.
You should use the .htaccess provided to you by Laravel. This should be your .htaccess file in the public/ folder (as per the Laravel Github repository)
<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>
public/.htaccess
If the public folder is not the root on your server, you'll also need to have this in an additional .htaccess (in the folder above public)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
/.htaccess
add this rule to keep assets from being routed to the router
RewriteEngine On
# Don't rewrite for css/js/img assets
RewriteRule ^assets/.* - [L]
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.my-site.com/$1 [R,L]

Resources