Laravel 4 infinite slash in url and still works - .htaccess

My problem is that these 2 url's behave the same way:
example.com/foo
example.com//////foo
They both get the requested page "foo" and load in the content from db.
But i don't like this.
So this is my htaccess:
<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>
And my routes are simple:
Route::get('/', 'PageController#index');
Route::get('/{slug}', 'PageController#show')->where('slug', '[\-_A-Za-z0-9]+');
Where can i set the second one to a 404? Is the problem in .htacces or in my routes?

Seems like a problem in Laravel, but you can redirect to whatever place you like by adding a rule to your .htaccess. Let's say you have a route /errors/404:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/{2,} [NC]
RewriteRule ^ /errors/404 [L,R=301]
EDIT
Just posted an issue in laravel/framework, to see what the core thinks about this: https://github.com/laravel/framework/issues/3608.

Related

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

htaccess redirect retains part of old url

Updating a site and have a ton of redirects.
The issue is that the new URL retains part of the old for all, as seen in the following example:
Redirect 301 /old/oldfile.html /new
redirects to:
http://url.com/new/oldfile.html
Aside from the redirects the htaccess contains the following:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
Options +FollowSymLinks
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Use mod_rewrite instead. This should go above the other rewrite rules.
RewriteEngine on
RewriteRule ^old/oldfile.html /new [R=301,L]

404 on all routes after applying SSL on Laravel 5 Project

I've Deployed my project on the server. It's working fine on the http but when I apply SSL certificate, and try to access URLs with https it shows 404 error.
I'm trying to find the solution since hours but no success. Here's my .htaccess file.
.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteBase /
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://serveravatar.com/%$1 [R,L]
</IfModule>
I am unable to find the Mistake. I also tried editing AppServiceProvider but no success!
What #SArnab said is correct, move it before your standard rules. Also, a better way of writing the rule would be:
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

htaccess Redirect All Traffic to https

I'm a beginner when it comes to doing stuff with htaccess so please be patient with my dumb question. I know that this has been addressed a lot (here, here, and here for example) but it doesn't seem to work for my situation. My browser displays a "redirect loop" error with this code in my htaccess file:
<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]
# Trying to redirect to https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</IfModule>
I know that this probably has something to do with the RewriteCond and the RewriteRule before I redirect to https, but I don't really know what I'm doing here and I don't know what to change.
Update:
Some more info that might be helpful:
When I remove the "redirect to https" code and manually type https://my.site.com it loads just fine.
Also, the redirect-to-https thing worked great before I accidentally deleted the .htaccess file in my directory.
The app that I'm trying to redirect resides in a subfolder of another app which also has an .htaccess file. Here's the code for that app:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteEngine On
</IfModule>
This is what firebug says:
Change order of your rules and keep 301 rules before other internal rewrite rule:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Trying to redirect to https
RewriteCond %{HTTPS} off
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>

Resources