I'm attempting to canonicalization my URL. However, when adding the relevant code, it breaks my other rewrite rules which allows me to hide the .html/ .php. It works fine when landing on the homepage but when you go to a page with www. and a file path at the end of the URL the page redirects to the homepage without www.

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?path=$1 [NC,L,QSA]

RewriteCond %{HTTP_HOST} ^www\.bespoke-apertures\.co.uk$ [NC]
RewriteRule (.*) https://bespoke-apertures.co.uk/$1 [R=301,L]
upvote
  flag
Difficult to understand the problem. What URL is not working and what is the error? Did you test from a new browser? – anubhava
upvote
  flag
@anubhava the website domain is bespoke-apertures.co.uk. When adding www. It now redirects removing the www. but now adds /public_html – Matt Hammond
upvote
  flag
@anubhava Through testing it turns out $1 is passing through the /public_html – Matt Hammond

2 Answers 11

Try this to redirect without www :

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]  
upvote
  flag
I've copied your replaced my line 5and6 with your code, however this hasn't seemed to effect anything, – Matt Hammond
upvote
  flag
check my updated answer – prakash tank
upvote
  flag
This has now stopped all links from working as it constantly redirects to /public_html – Matt Hammond
up vote 1 down vote accepted

Have it like this in different order:

RewriteEngine on

RewriteCond %{HTTP_HOST} ^www\.(bespoke-apertures\.co.uk)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?path=$1 [L,QSA]

Make sure to test this in a new browser to avoid old cache.

1 upvote
  flag
Thank you! It also turned out that my file was in the incorrect folder, but all fixed now thank you! – Matt Hammond

Not the answer you're looking for? Browse other questions tagged or ask your own question.