.htaccesshttp-redirectmod-rewritemod-alias

HTACCESS 301 redirect keep sending to the wrong page


I am trying to redirect an old page from a website I have redesigned, to the new one, but it's not working.

Here's my 2 lines of code in the .htaccess file regarding that domain:

Redirect 301 /deaneco http://solutionsgtr.ca/fr/deaneco/accueil.html
RewriteRule ^/deaneco/contact http://solutionsgtr.ca/fr/deaneco/contact.html [R=301,L,QSA]

If go on the solutionsgtr.ca/deaneco/contact URL, it gives me the following page:

http://solutionsgtr.ca/fr/deaneco/accueil.html/contact

The first rule works though (deaneco/ to solutionsgtr.ca/fr/deaneco/accueil.html).

I feel like both lines are being mixed together and are giving me the wrong page, that doesn't exist so I get a 404 error.


Solution

  • There are a couple of issues here:

    You should avoid mixing redirects from both modules since they execute independently and at different times during the request (mod_rewrite executes first, despite the apparent order of the directives).

    Either use mod_alias, ordering the directives most specific first:

    Redirect 301 /deaneco/contact http://solutionsgtr.ca/fr/deaneco/contact.html
    Redirect 301 /deaneco http://solutionsgtr.ca/fr/deaneco/accueil.html
    

    NB: You will need to clear your browser cache, since the erroneous 301 (permanent) redirect will have been cached by the browser. Test with 302 (temporary) redirects to avoid potential caching issues.

    OR, if you are already using mod_rewrite for other redirects/rewrites then consider using mod_rewrite instead (to avoid potential conflicts as mentioned above):

    RewriteEngine On
    
    RewriteRule ^deaneco/contact$ http://solutionsgtr.ca/fr/deaneco/contact.html [R=301,L]
    RewriteRule ^deaneco$ http://solutionsgtr.ca/fr/deaneco/accueil.html [R=301,L]
    

    The QSA flag is not required, since the query string is passed through to the substitution by default.

    The order of the RewriteRule directives are not important in this instance, since they match just that specific URL.


    If go on the solutionsgtr.ca/deaneco/contact URL

    If you are redirecting to the same host then you don't need to explicitly include the scheme + hostname in the target URL, since this will default.