301 redirect rule through htaccess issue - .htaccess

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]

Related

301 Redirect in htaccess to remove URL Parameter? And redirect to original URL

I have following pattern of URLs in some of my WordPress site
abc.com/xolo-lt900-price-in-india.html? m=1
and
abc.com/xolo-lt900-price-in-india.html? m=0
I want to remove part which is after parameter value and 301 redirect it to main/original URL.
So,
bc.com/xolo-lt900-price-in-india.html?m=1
Shall 301 redirect to
abc.comprice4india.co.in/xolo-lt900-price-in-india.html
And in same way
abc.com/xolo-lt900-price-in-india.html? m=0
Shall 301 redirect to
abc.com/xolo-lt900-price-in-india.html
I have the following code but its not working.
RewriteCond %{QUERY_STRING} m [NC]
RewriteRule ^$ /? [R=301,L]
Thanksfully the old code from #Anubhava is working now..
RewriteCond %{QUERY_STRING} ^m=[01]$ [NC]
RewriteRule ^(.*)$ /$1? [R=301,L,NC]

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.

Replace word within url and redirect

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]

301 from mobile=N in htaccess

I wanted to make a 301 from URL with ?mobile=N parameter to URL without that parameter. Google is indexing this URL and I think that 301 is the best way to fix that
eg.
FROM
www.example.com/?mobile=N
TO
www.example.com
FROM
www.example.com/example/example.com?mobile=N
TO
www.example.com/example/example.com
You can use in your .htaccess:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^mobile=N$ [NC]
RewriteRule ^ %{REQUEST_URI}? [R=301,L]

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]

Resources