2

I recently managed to direct all URLs from non-www to www as I had an SEO error regarding duplicate websites.

Once I have applied www rules then all my URLs start showing index.php?page= which doesn't look pretty.

Here are my current .htaccess rules:


RewriteEngine On

RewriteBase /

RewriteRule ^([-a-zA-Z0-9]+)$ index.php?page=$1

RewriteCond %{HTTP_HOST} !^www.example.com$
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

RewriteEngine On
RewriteBase /

# Removes index.php?page=$1
RewriteCond %{THE_REQUEST} ^.*/index.php?page=$1
RewriteRule ^(.*)index.php?page=$1$ https://www.example.com/$1 [R=301,L]
RewriteRule ^(.*)index$ https://www.example.com/$1 [R=301,L]

I have tried the code above and it didn't work. Is there anything I am missing?

MrWhite
  • 11,643
  • 4
  • 25
  • 40

1 Answers1

0

Your rules are in the wrong order. Your external non-www to www redirect needs to be before the internal rewrite, immediately after the first RewriteBase directive.

As a general rule, "redirects" should always go before "rewrites".

UPDATE: You will need to make sure you've cleared your browser cache (and any intermediary caches) since the erroneous 301 (permanent) redirect will have certainly been cached by the browser.

In summary, your rules should look like this:

RewriteEngine On

RewriteBase /

# Redirect non-www to www
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule (.*) https://www.example.com/$1 [R=301,L]

# Internal rewrite for pages
RewriteRule ^([-a-zA-Z0-9]+)$ index.php?page=$1 [L]

Any literal dots in the regex need to be backslash escaped. And you should include the L flag on the rewrite should you add additional rules later.

(NB: I assume you are linking to URLs of the form /this-is-my-page and not /index.php?page=this-is-my-page - otherwise you need to change your internal links.)

MrWhite
  • 11,643
  • 4
  • 25
  • 40
  • I have just organized the order but my links are still showing as:https://www.culture-insider.com/?page=ExperienceMorocco instead of https://www.culture-insider.com/ExperienceMorocco – Siham Lahmine Jun 07 '21 at 16:05
  • @SihamLahmine You'll need to make sure you've cleared your browser cache and any intermediary caches since the erroneous 301 (permanent) redirect will have been cached. You should also include the `L` flag on the rewrite for good measure. I assume you have also removed the last couple of rules as well? – MrWhite Jun 07 '21 at 16:37
  • @SihamLahmine I've updated my answer to clarify how the directives should look. Also, I assume you are linking to URLs of the form `/this-is-my-page` and not `/index.php?page=this-is-my-page`? – MrWhite Jun 07 '21 at 17:36
  • thank you so much, this is clear indeed and easy to test. Can I ask please how would I know from where my URLs are linked to answering the question correctly? – Siham Lahmine Jun 07 '21 at 18:27
  • MrWhite, I think I have fixed it thanks to you, I will wait until tomorrow to make sure - huge thanks for your assistance! – Siham Lahmine Jun 07 '21 at 20:20
  • @SihamLahmine "how would I know from where my URLs are linked to" - I'm referring to the HTML anchor in your source code - _your internal links_. ie. Your anchors should be for the form `` and not ``. – MrWhite Jun 08 '21 at 15:37
  • @SihamLahmine "I think I have fixed it thanks to you" - You're welcome. If this helped answer your question then please mark it as accepted (checkmark below the voting arrows, next to my answer) - to help other readers and to remove the question from the unanswered question queue. You can also upvote answers you find useful. Thanks, much appreciated. :) – MrWhite Jun 08 '21 at 15:40