301 Redirect with cet= parameter - .htaccess

I would like to make a 301 redirect of a url in http://website.com/example-old/?cet=3132 format to a page https://www.website.com/example-new/.
I have tried several times via .htaccess with the classic method:
redirect 301 /example-old/?cet=3132 https://www.website.com/example-new/
The redirect works, but I don't get what I want. In fact the final url becomes https://www.website.com/example-new/?cet=3132.
In short, the ?cet=3132 doesn't disappear and this is not good for me.
I found this invaluable resource which recommends a mod_rewrite method: 301 redirect for old urls with language parameter
Sure I'll try it, but I was wondering: will it work even with the cet=3132 parameter?
Thanks to those who can answer, best regards.

Sure you can use the rewriting module for that:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^cet=3132$
RewriteRule ^/?example-old/?$ https://www.example.com/example-new/ [QSD,L,R=301]
The QSD flag will take care to remove the query string during the redirection.
It is a good idea to start out with a R=302 temporary redirection and to only change that to a R=301 permanent redirection once everything works as intended. That prevents nasty caching issues.

Related

Htaccess redirects with parameters

I am trying to migrate a shop to another system, and would like to redirect my directories.
E.g. www.oldshop.eu/stuff to www.newshop.eu/stuff/
That I do by using
redirect 301 /stuff/ www.newshop.eu/stuff/
That works well, however my current shop has pages of the directories indexed, like:
www.oldshop.eu/stuff/?p=2
That I dont want to transfer to the newshop, however I can see on search console that this is being done. Seems my redirect takes everything after the /stuff/ and just putting it over?!
How can i avoid this so that all url with ?p= or other parameters are being avoided?
Br. Brian
You can use the rewriting module here:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?old\.example\.com$
RewriteRule ^/?stuff/?$ https://new.example.com/stuff/ [QSD,R=301,L]
PS: It is a good idea to start out using a R=302 temporary redirection and only change that to a R=301 permanent redirection once everything works as desired. That prevents nasty caching issues on the client side.

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

Why does this redirect apply?

I have a simple Redirect in my htaccess file:
Redirect 301 /foobar /johndoe/foobar
unfortunately, this url:
/foobar/barfoo also gets redirected - why? In my understanding, only /foobar should be redirected when using the Redirect command, shouldnt it?
I feel that this is more comfy than writing RewriteRules
As it turns out, Redirect seems to just check if the URL to check is in the beginning of the current path (at least my tests say that, the documentation is not 100% clear about it).
But, to avoid using RewriteRule (since it might be overkill), simple RedirectMatch also works:
RedirectMatch 301 "^/foobar$" "/johndoe/foobar"
I would still be thankful for additional advice, whether this isnt possible to solve without "regex" and/or RewriteRule

301 redirect throws an error saying the page being redirected does not exist

i want to redirect from a page at /roofing/bellevue/index.php to /bellevue-roofing.php I entered the following:
Redirect 301 /roofing/bellevue/index.php http://www.emeraldstate.com/bellevue-roofing.php
into .htaccess in the root directory.
The result of entering www.emeraldstate.com/roofing/bellevue/index.php is:
The requested URL /roofing/bellevue/index.php was not found on this server.
I have checked and rechecked various sources on formatting Redirects and everything seems correct. Can anyone provide a little guidance?
The correct implementation is to avoid specifying the domain name as part of the redirect. Whilst this will more than likely make no difference to your setup, I'm mentioning it anyway.
You'll be better off using RedirectMatch or mod_rewrite (which I prefer):
RedirectMatch 301 ^/roofing/bellevue/index.php$ /bellevue-roofing.php
Or use mod_rewrite (make sure that the extension is enabled, which it generally is):
RewriteEngine On
RewriteRule ^/roofing/bellevue/index.php$ /bellevue-roofing.php [R=301,L]

Why does this htaccess work for a bit, and then doesnt?

I'm trying to redirect people to a new url of the login page,
It used to work, I made a few changes though, which kind of failed as I'm still trying to learn how to use htaccess,
Anyhow I brought the very first code back, and for some awkward reason, it won't work anymore,
Here's the code:
# external redirect using R=301 to /login from /index.php?act=Login
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(index\.php/?|)\?act=Login(&|\s) [NC]
RewriteRule ^/login? [R=301,L]
Is there any better way to achieve what I need?
Thanks!
Check the regular expression in the RewriteCond line. There is (index\.php/?|). The pipe character is either to much and should be removed or you missed the second option for the sub-pattern.
Alternatively you should check whether a simple Redirect instruction will be enough instead of using complex rewrite rule for a possibly simple task.
Basic syntax of redirect is:
Redirect [status] <old-url> <new-url>
for your purposes the following should work:
Redirect 301 /index.php?act=Login /login

Resources