Aleksandra Chuprova
Aleksandra Chuprova

Reputation: 503

redirect all the pages from non www to www in htaccess

I know that there is a lot info out there on redirects from non www to www domain. But my problem is that I can manage the redirect of the home page, but not the subpages.

You could see the example here So when I enter the url like ourenglishclass.eu/fill-in-text-5th-6th-grade the redirect happens to the www.ourenglishclass.eu/index.php

I can see that there is probably more rewrite rules which cause it to behave this way, but I cannot find what, or how can I fix this

These are the rules which redirecting to /index.php

 #
 RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
 #
 # If the requested path and file is not /index.php and the request
 # has not already been internally rewritten to the index.php script
 RewriteCond %{REQUEST_URI} !^/index\.php
 # and the requested path and file doesn't directly match a physical file
 RewriteCond %{REQUEST_FILENAME} !-f
 # and the requested path and file doesn't directly match a physical folder
 RewriteCond %{REQUEST_FILENAME} !-d
 # internally rewrite the request to the index.php script
 RewriteRule .* index.php [L]

I have tried these redirect rules from non-www to www:

  #RewriteCond %{HTTP_HOST} !^www\. [NC]
  #RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

and (this is because i'm testing also with https)

  # Redirect to www
  RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
  RewriteCond %{HTTPS}s ^on(s)|
  RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Hopefully someone can point the way out

Upvotes: 3

Views: 1062

Answers (1)

anubhava
anubhava

Reputation: 785058

Problem appears to be due to the fact that you are placing redirect rule below your front controller rule that forwards everything to index.php hence making REQUEST_URI equal to /index.php.

Have your rule like this:

RewriteEngine On

# Redirect to www
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

#
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
#
# If the requested path and file is not /index.php and the request
# has not already been internally rewritten to the index.php script
RewriteCond %{REQUEST_URI} !^/index\.php
# and the requested path and file doesn't directly match a physical file
RewriteCond %{REQUEST_FILENAME} !-f
# and the requested path and file doesn't directly match a physical folder
RewriteCond %{REQUEST_FILENAME} !-d
# internally rewrite the request to the index.php script
RewriteRule ^ index.php [L]

Make sure you clear your browser cache before testing this change.

Upvotes: 4

Related Questions