Laravel redirects to 404 error page if URL has www prefix - laravel

I'm having project in Laravel 6.12. I've hosted my app in live server and having SSL configured.
It works well when I access with https://example.com, but when I try to access with https://www.example.com it shows laravel's default 404 error page.
It means, it is accessing the project directory, loading css, js etc but showing 404 error page.
my .htaccess has following lines
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R,QSA,L]
Could anyone help to get rid of this?

replace your .htaccess with this
if try https://example.com it will force redirect to https://www.example.com
This is true to SEO
RewriteCond %{HTTPS} =on
# check if not have www
RewriteCond %{HTTP_HOST} !^www\. [NC]
# force redirect to www
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Related

.htaccess Not Redirecting To requested page

I built an application with laravel and redirected all request from my old-domain.com to my new-domain.com using this line of .htaccess
RewriteCond %{HTTP_HOST} ^(www\.)?old-domain\.com$
RewriteRule (.*) https://www.new-domain.org/$1 [R=301,L]
But when I visit https://www.new-domain.org/requestedPage I get redirected to https://www.new-domain.org/index.php instead of to https://www.new-domain.org/requestedPage.
Thanks.

Can Director::forceWWW() and Director::forceSSL() be used together in SilverStripe?

I have a SilverStripe website which I'd like to always use the www domain prefix and always use https.
So:
http://example.com
http://www.example.com
https://example.com
Would all redirect to:
https://www.example.com
However when I put the following lines in /app/_config.php:
Director::forceWWW();
Director::forceSSL();
I get a redirect loop. The same thing happens if I swap the order:
Director::forceSSL();
Director::forceWWW();
Does this mean that Director::forceWWW() and Director::forceSSL() can't be used together?
What configuration should I use to get my desired outcome?
I think you are correct in that forceWWW() and forceSSL() can't be used together.
If our site is on an Apache server we can add redirect rules to our root .htaccess file to do these two redirects:
# ...
<IfModule mod_rewrite.c>
SetEnv HTTP_MOD_REWRITE On
RewriteEngine On
# Redirect non www to www
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
# Redirect non https to https
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# ...

301 Redirect Dynamic HOST includes DOCUMENT_ROOT

Based on what I read on Apache I used the following example they provided to do a 301 Redirect on all my web sites.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
This is not quite working as they said it would. If I try www.domain.com it works. If I try domain.com I get www.domain.com//home/www/public_html/www.domain.com
Looks like it wants to include the DOCUMENT_ROOT in the redirect. Am I better off to create an individual .htaccess for each web site?
What is faster to run - Apache or HTACCESS?
Try this instead. Make sure you include the RewriteBase /
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
If you still get the old result, your previous 301 redirect is probably cached, retest in Private (Incognito) Browsing Mode.
Using Apaches httpd.conf is faster since accessing the .htaccess file adds a small overhead—Apache checks every directory—and parent directory—for the file and it will be loaded for every request.
Using the httpd.conf is better when you have access to it. Use .htaccess if you don't have access to the main configuration file.

.htaccess redirect www to non-www url doesn't not apply on the other link than the index

I've a Mac server and everything working fine
I'm running a wordpress and have .htaccess to rewrite the rule for custom link and so on
right now I'm having a trouble once I visit my site
http://www.mysite.com/anypage.html This doesn't work and give me page not found error
but if I visit same page but removed the www http://mysite.com/anypage.html this will work fine
So I thought I will make sure if this happen to all the pages and it was having the problem with all the pages except the home page so if I visit http://www.mysite.com or http://mysite.com neither one will work
I tried with many .htaccess rewrite rules and non of them word
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.*)$ %{HTTP_HOST}$1 [C]
RewriteRule ^www\.(.*)$ http://$1 [L,R=301]
Please suggest what is the problem.
Thanks
All your www will be redirected to now www URLs.
Just do this and try:
RewriteCond %{HTTP_HOST} ^www\.(mysite\.com)/?$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [L]

mod_rewrite - some requests being rewritten should produce a 404 but don't

I've been working on creating seo friendly urls for my site and have done this successfully with the following rules:
RewriteEngine on
RewriteBase /
#redirect non www to www
RewriteCond %{HTTP_HOST} ^example.co.uk [NC]
RewriteRule ^(.*)$ http://www.example.co.uk/$1 [L,R=301]
# Redirect any request with page=var to /var/ format
RewriteCond %{QUERY_STRING} ^page=(.+[^/])$ [NC]
RewriteRule ^index\.cfm$ http://%{HTTP_HOST}/%1/? [R=301,L,NC]
# If not an existing file or directory rewrite any request
# to page var.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+[^/])/$ index.cfm?page=$1 [QSA,L]
My problem is that because of the last rule (I think), any root dir request is made into a friendly url such as /pagethatdoesntpointanywhere/ whether it points anywhere or not. Now I'd be happy with a 404 but it's not doing that it's just displaying the homepage.
I've also tried adding a blanket 404 rule:
# 404 files that don't exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .+ /notfound.html [R=404,L,NC]
But that makes all of the friendly urls 404s as well.
Could someone explain where I am going wrong here please?
Use ErrorDocument 404 /notfound.cfm to display not found error page when Apache cannot find the file (replace notfound.cfm by your file).
In index.cfm, if value of page parameter is unknown (e.g. pagethatdoesntpointanywhere), display 404 error page (use the same/similar code as notfound.cfm). It is the right place to do considering your rewrite rules and the fact that you checking which page to display here anyway. Lots of products/frameworks work in similar fashion (for example: WordPress).
Use both #1 and #2. Number #2 will work for 1-folder deep URLs (e.g /meow/) while #1 will catch any other URLs (e.f. /meow/kitten/ or /meow/wuf/oink.css).

Resources