Search code examples
apache.htaccessurl-rewritingno-www

URL re-write not working without www


I am having some issues with URL re-writing that I cannot figure out. Here's the problem.

This URL IS NOT redirecting properly:
http://domain.com/index.php?en=oldpage

HOWEVER, this URL IS redirecting properly:
http://www.domain.com/index.php?en=oldpage

The only difference in the url that is not redirecting properly is the absence of the www.

Here is the re-write I am using:

RewriteCond %{QUERY_STRING} ^en=oldpage
RewriteRule ^(index.php/|)$ /newpage.html? [R=301,L]

I also have this re-write BEFORE other re-writes, to handle url's without "www.":

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

Why will it not re-direct without the "www." ? Any help would be much appreciated. Thank you.


Solution

  • I guess there is some code/rule below those rules adding query string back to your URLs.

    Try these rules instead based on THE_REQUEST variable which doesn't get overwritten:

    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    
    RewriteCond %{THE_REQUEST} \s/+(?:index\.php)?\?en=oldpage [NC]
    RewriteRule ^ /newpage.html? [R=301,L]
    

    Make sure to test it after clearing your browser cache.