Replace word within url and redirect - .htaccess

I am trying to replace word with another word in url and redirect but its not wokring due to query string.
Here is eg. Here is the url and i want to redirect it
https://www.custesitetest.com/test_calculator.php?tf=to&loc=staines
to
https://www.custesitetest.com/autoqoute?tf=to&loc=staines
RedirectMatch ^/test_calculator.php/(.*)$ https://www.custesitetest.com/autoquote/$1

You can use this rule in your root .htaccess:
RedirectMatch 301 ^/test_calculator.php?$ /autoquote

Try it like this with mod_rewrite,
RewriteEngine On
RewriteCond %{REQUEST_URI} ^test_calculator.php
RewriteCond %{QUERY_STRING} ^(.+)
RewriteRule ^ autoquote?%1 [R=301,L]

Related

htaccess 301 permanent redirect using query string

I'm trying to do a 301 with a query string.
I want to redirect
www.mydomain.com/page1.php?id=12
To
www.mydomain.com/page2.php?id=12
I've already tried this in .htaccess:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^page_id=([0-9]+)$
RewriteRule ^page2.php?page_id=%1 [R=301,L]
You may use this redirect rule:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=\d+$
RewriteRule ^page1\.php$ /page2.php [R=301,L,NC]
Query string will be automatically copied over to new URL.

How redirect urls with special url?

I want to redirect this type url to my home page
http://www.mywebsite.com/index.php?showuser=
to
http://www.mywebsite.com/
Can I do this with .htaccess? How to do it. I tried this but works nothing.
redirectMatch 301 /index.php?showuser= http://www.mywebsite.com
And
RedirectMatch 301 /index.php\?showuser\= http://www.mywebsite.com
You can't match against the query string in the RedirectMatch regex. You need to use mod_rewrite and the %{QUERY_STRING} variable:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^showuser=
RewriteRule ^index\.php$ /? [L,R=301]
Neither Redirect or RedirectMatch can match the query string. The ? in RedirectMatch actually means "the previous character 0 or 1 times", and would be matched against "index.php". Please note that you are not using the dot correctly too.
You can solve this by using mod_rewrite. Make sure that it is enabled, and that you allow override of FileInfo. Then use the following directives:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^showuser=$
RewriteRule ^index\.php$ / [R,L]
Change [R] to [R=301] if you wish to make the redirect permanent after testing everything works as expected.

url redirect exact match including params

i would like to make a 301 redirect and from an old website using the exact exact url with no extra parameters.
example:
/en-direct.php?page=7
to go to:
http://www.example.org/news/
and page:
/en-direct.php?page=8
to go to:
http://www.example.org/awesome-but-totally-different-page/
i used:
redirectMatch 301 ^/en-direct.php$ http://www.example.org/different-page/
redirectMatch 301 ^/en-direct.php?page=7$ http://www.example.org/news/
redirectMatch 301 ^/en-direct.php?page=8$ http://www.example.org/awesome-but-totally-different-page/
however: i get http://www.example.org/different-page/ every time with all the parameters from the redirect page (example - http://www.example.org/different-page?page=7 )
any help will be much appreciated.
In order to match against the query string, you need to use mod_rewrite:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^page=7($|&)
RewriteRule ^en-direct\.php$ http://www.example.org/news/ [L,R=301]
RewriteCond %{QUERY_STRING} ^page=8($|&)
RewriteRule ^en-direct\.php$ http://www.example.org/awesome-but-totally-different-page/ [L,R=301]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^en-direct\.php$ http://www.example.org/different-page/ [L,R=301]

Redirect query url to new page

I have this url
www.example.com/nothing_here?registration=disabled
and I want to redirect it to
www.example.com/errors/418.php
I cannot get rid of the nothing_here part of the url. How to do this?
Thanks
In the htaccess file in your document root, add:
RedirectMatch 301 ^/nothing_here$ /errors/418.php?
or using mod_rewrite:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^registration=disabled$ [NC]
RewriteRule ^nothing_here$ /errors/418.php [L,R=301]

301 redirect rule through htaccess issue

I want to 301 redirect from
domain.com/?test-article.php
to
domain.com/full-article/test-article.php
Here is my code what i am trying to use and its not working.
redirect 301 /?test-article.php http://domain.com/full-article/test-article.php
or
rewriteRule /?test-article.php http://domain.com/full-article/test-article.php [R=301,L]
if i use without '?' then it re-directs from
/test-article.php
to
http://domain.com/full-article/test-article.php
So something has to do with '?'
You can't match against the query string using a pattern of a Redirect or RewriteRule. You need to use the %{QUERY_STRING} variable and mod_rewrite:
RewriteCond %{QUERY_STRING} ^test-article$
RewriteRule ^/?$ http://domain.com/full-article/test-article.php [R=301,L]

Resources