how to use htaccess redirect

Question: Do you know how we can redirect where the old website has query on it

example: Redirect 301 /detail.asp?ID=130 http://allpowersolution.com/used-gas-generators/

Tried to implement the following but it didn’t help:

https://simonecarletti.com/blog/2009/01/apache-query-string-redirects/

Please check the chat for more info.

  • Lindeni Mahlalela
    • Recruit

    Hello Argo.

    I hope you are doing great today and thank you for contacting us. I have examined your question and the resource you linked to and I came up with the following code you can paste in your .htaccess. I tested it on my install and it works as it is.

    RewriteEngine On
    RewriteCond %{REQUEST_URI} ^/detail.asp$
    RewriteCond %{QUERY_STRING} ^ID=([0-9]*)$
    RewriteRule ^(.*)$ http://yourdomain.com/used-gas-generators/ [R=302,L]

    Replace yourdomain.com with your actual domain.

    I have found that this code does not work if you paste it below all WordPress rules because the WordPress rewrite rules will take effect before it gets to the lines above. If you pasted below the WordPress rules then you will always get redirected to a “404 Not found” page, but if pasted above the WordPress rules, those lines will be executed and if the RewriteCond is met the rule will take effect, otherwise it will proceed to the WordPress rules.

    So, this means you should paste this:

    RewriteCond %{REQUEST_URI}  ^/detail.asp$
    RewriteCond %{QUERY_STRING} ^ID=([0-9]*)$
    RewriteRule ^(.*)$ http://yourdomain.com/used-gas-generators/ [R=302,L]

    Just below the line:

    RewriteEngine On

    You should only have one ‘RewriteEngine On’ and all other files will follow after your custom rewrite rule. Please try that and let us know if it works or not. We will be happy to know that it works and if not, we will be happy to assist in any way possible.

    Cheers,

    Mahlamusa

  • Lindeni Mahlalela
    • Recruit

    Hi Argo.

    I hope you are doing great today and thank you for getting back to us. I am not sure if there is a simpler way but here is what I tried and it works.

    You should have one line with the query_url parameter like this:

    RewriteCond %{REQUEST_URI} ^/detail.asp$

    Then repeat the following for all the permalinks:

    RewriteCond %{QUERY_STRING} ^ID=XYZ
    RewriteRule ^(.*)$ http://yourdomain.com/page-name-here/ [R=301,L]

    Replace XYZ with the ID of the old page, ‘yourdomain.com/page-name-here’ with the actual permalink. So, with the above urls, you can try the following:

    RewriteEngine On
    RewriteCond %{REQUEST_URI} ^/detail.asp$

    RewriteCond %{QUERY_STRING} ^ID=XYZ
    RewriteRule ^(.*)$ http://yourdomain.com/page-name-here/ [R=301,L]

    RewriteCond %{QUERY_STRING} ^ID=130
    RewriteRule ^(.*)$ http://yourdomain.com/used-gas-generators/ [R=301,L]

    RewriteCond %{QUERY_STRING} ^ID=130
    RewriteRule ^(.*)$ http://yourdomain.com/used-gas-generators/ [R=301,L]

    RewriteCond %{QUERY_STRING} ^ID=131
    RewriteRule ^(.*)$ http://yourdomain.com/used-diesel-generators/ [R=301,L]

    RewriteCond %{QUERY_STRING} ^ID=133
    RewriteRule ^(.*)$ http://yourdomain.com/contact/ [R=301,L]

    #All WordPress Rules Below

    I hope this helps. If you have any questions or concerns, please let us know and we will be happy to assist.

    Please enjoy the rest of your day.

    Cheers,

    Mahlamusa

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.