Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Support

Redirects from www pages to /index.php

Solved by Thomas View solution

Started by Thomas 3 years ago · 4 replies · 918 views
3 years ago

Hi all.

On a simple Quark install without much customization at system config level, all is working fine, including all redirections to https://, except redirections from WWW to non WWW for inside pages only.

Example:
Page
https://www.mydomain.com/mypage will redirect to
https://mydomain.com/index.php :frowning_face: instead of
https://mydomain.com/mypage

Same behavior for
http://www.mydomain.com/mypage and
www.mydomain.com/mypage,
and same for all inside pages.

How can WWW be redirected to /index.php ??? :frowning_face:

Redirections in .htaccess are as follows, identical to on my other Grav sites :

TXT
# Remove trailing slash from non-filepath urls
RewriteCond %{REQUEST_URI} /(.+)/$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ https://mydomain.com/%1 [R=301,L]

# Include trailing slash on directory 
RewriteCond %{REQUEST_URI} !(.+)/$
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+)$ https://mydomain.com/$1/ [R=301,L]

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]

Error pages, as long as there is no WWW, correctly redirect from
nothing or http:// or https:// mydomain.com/error-slug to
https://.mydomain.com/error-slug.

I have never seen this on a Grav site. I suppose something is happening in Grav settings but a few hours on this did not help me find the culprit.
This site have just a few special plugins and disabling them do not help. All updated. I tried to look in the webmaster console and Grav Clockwork debugger and, as much as I understand those tools, I could not see information on this.

Any brilliant idea?
Thank you if you have a hint.

👍 1
last edited 03/03/23 by Thomas
3 years ago

I put the question on the side and have not yet found the solution. Still interested…

3 years ago

For me the htaccess tester by madewithlove has been a valuable tool to test htaccess rewrite rules.

When the htaccess file of the site which behaves bad is identical to your other sites then the difference must be in the Grav config. Especially look for any redirection routes.

Without you sharing these config files here there is not much we can do to help.

👍 2
3 years ago Solution

Thank you @bleutzinn for your suggestion.
As there were many red errors in my test, I decided to start from scratch with default Grav .htaccess content and some step by step help from ChatGPT to add my rules.
It seems that the important suggestion was to place these kind of domain level redirections before the default Grav specific (Quark theme) rules.
I now have these rules at the beginning of .htaccess file:

TXT
RewriteEngine On

# Redirect to HTTPS and remove www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Remove trailing slash from inside pages
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [L,R=301]

# Remove possibility to view indexes of files
Options -Indexes

… followed by default Grav specific rules.

I wanted all pages to be in https://example.com/mypage without trailing slashes.
It works as expected and htaccess tester now do not have any red warnings anymore.

My other Grav sites indeed had these rules at the beginning of .htaccess.

👍 2

Suggested topics

Topic Participants Replies Views Activity
Support · by Thomas, 1 week ago
2 53 10 hours ago
Support · by Anna, 3 days ago
2 60 13 hours ago
Support · by Justin Young, 14 hours ago
1 30 14 hours ago
Support · by Duc , 1 week ago
2 65 5 days ago
Support · by Colin Hume, 1 week ago
2 56 5 days ago