HTACCESS 301 redirect keep sending to the wrong page

358 views Asked by At

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.

1

There are 1 answers

1
MrWhite On BEST ANSWER

There are a couple of issues here:

  • The Redirect directive (part of mod_alias) is prefix-matching and everything after the match is appended on the end of the target URL. This explains the redirect you are seeing.

  • The RewriteRule (mod_rewrite) pattern ^/deaneco/contact will never match in a .htaccess context since the URL-path that is matched does not start with a slash. So, this rule is not doing anything currently.

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.