Score:2

When changed to WWW, all URLs showing index.php?page= how to remove index.php?page=?

us flag

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?

Score:0
kz flag

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.)

us flag
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
kz flag
@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?
kz flag
@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`?
us flag
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?
us flag
MrWhite, I think I have fixed it thanks to you, I will wait until tomorrow to make sure - huge thanks for your assistance!
kz flag
@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 `<a href="/this-is-my-page">` and not `<a href="/index.php?page=this-is-my-page">`.
kz flag
@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. :)
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.