htaccess 301 redirect - Remove query string - php

I would like to redirect the urls which have query strings to the top page like this
from
https://cococo.com?abc=123
to
https://cococo.com
because when i type https://cococo.com?abc=123 it also display the top page so I think it's not seo-friendly.
how to whrite the .htaccess to solve this problem?
the follow one is wrong.
RewriteCond %{QUERY_STRING} .+
RewriteRule ^\?(.*)$ %{ENV:REWRITEBASE}?$1 [L,R=301]

You can use this rule to remove query string only on landing page:
RewriteEngine On
RewriteCond %{QUERY_STRING} .
RewriteRule ^/?$ /? [L,R=301]

Related

redirect url exact match with htaccess like case sensitive

redirect url exact match with htaccess not redirect if query string or anything after index.php
Redirect This Page: https://example.com/demo/index.php
To Page: https://example.com/ (home page)
But Do Not redirect: https://example.com/demo/index.php/*
Do NOT redirect: https://example.com/demo/index.php/password
Do NOT redirect https://example.com/demo/index.php?m=page
not redirect if any other combination
only redirect https://example.com/demo/index.php to https://example.com/
this script not working
RewriteEngine On
RewriteBase /
RewriteRule ^demo/index.php /demo/index.php?m=page[L,NC,END]
RewriteRule ^demo/index.php$ https://example.com/ [L,R=301]
only redirect https://example.com/demo/index.php to https://example.com/
If you only want to redirect that exact URL, without a query string then you need to reverse your existing rules and include an additional condition that checks that the QUERY_STRING is empty. The RewriteRule only matches against the URL-path, which excludes the query string.
You are also missing a space before the flags argument in the first rule.
For example:
# Redirect exact URL to home page
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^demo/index\.php$ / [R=302,L]
# Redirect other URLs with query string OR path-info
RewriteRule ^demo/index\.php /demo/index.php?m=page [NC,L]
You don't need L and END.
Test with 302, and change to 301 only once you have confirmed it works OK. To avoid caching issues.
You may use these rules in site root .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+demo/index\.php\s [NC]
RewriteRule ^ / [L,R=301]
RewriteRule ^demo/index\.php$ $0?m=page [QSA,NC,L]
This was created by htaccess redirect generator I use very often.
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^demo/index\.php$ https://example.com/? [R=301,L]
Maybe "?" sign is redundant because of empty query string match.
Not sure, but You also can try to add
RewriteCond %{REQUEST_METHOD} !=POST
in case You can't log in. All together:
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^demo/index\.php$ https://example.com/? [R=301,L]

WordPress : 301 redirection not working

I am using the following code to redirect a dynamic URL to a new dynamic URL, under the same domain in wordpress:
RewriteRule ^view-company/?id=(.+?)&name=(.+?)$ view-company/$1/$2/? [L,R=301,NC]
I want to redirect https://www.mydomains.com/view-company/id=1&name=abc to https://www.mydomains.com/view-company/1/abc
But this is not redirecting me to new url. Please help me.Thanks in advance
You can not match against query strings in RewriteRule's pattern, you need to match against %{QUERY_STRING} Variable using a RewriteCond
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=([^&]+)&name=([^&]+)$
RewriteRule ^view-company/?$ https://www.mydomains.com/view-company/%1/%2? [L,R=301]
You can also manipulate querystrings using %{THE_REQUEST} Variable
RewriteEngine on
RewriteCond %{THE_REQUEST} /view-company/\?id=([^&]+)&name=([^&\s]+)
RewriteRule ^ https://www.mydomains.com/view-company/%1/%2? [L,R=301]

How to Redirect page after removing Query String

I have bunch of old website URLs that are I am trying to redirect to new destination , right now I am trying to remove query string from old URLS and then redirect to new URL with its parameters like
Previous: www.domain.com/ranges.php?reqinfo=some-data-info
Required: www.domain.com/info/ranges/some-data-info
I am trying this in .htaccess
RewriteCond %{QUERY_STRING} ^\ranges\.php\?reqinfo=(.*)$ [NC]
RewriteRule ^info/ranges/%1? [L,R=301]
I have also tried one other rule but it also remove all peramaters as well
RewriteRule ^ranges\.php$ /info/ranges/$1? [L,R=301]
can someone please help.
ranges.php is part of your REQUEST_URI hence you cannot match in using QUERY_STRING condition. Use it as:
RewriteCond %{QUERY_STRING} ^reqinfo=([^&]+) [NC]
RewriteRule ^ranges\.php$ /info/ranges/%1? [L,R=301,NC]

.htaccess not redirecting when url contains ? or =

Can anyone tell me how to redirect if incoming URL contains certain text as having real trouble with this..
For instance:
RedirectMatch 301 /files/9453041/batch-BiDA http://secondsite.com (THIS WORKS)
RedirectMatch 301 /?load=/files/9453041/batch-BiDA http://secondsite.com (THIS DOESNT WORK)
Only the first of the above two redirects work, not sure why but the second containing the /?load= doesnt redirect. Can someone either tell me what i need change on the second line for it to work?
Or perhaps I was thinking if it couldnt be made to work if it would be possible to redirect every incoming url that has "mysite.com/?load=/whatever/whatever" to "mysite.com/whatever/whatever"?
Many thanks for any help...
In order to match query strings (everything after the ?), you need to match against the %{QUERY_STRING} variable using a rewrite condition:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^load=/files/9453041/batch-BiDA
RewriteRule ^/?$ http://secondsite.com/ [L,R=301]
You can also use the [OR]:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^load=/files/9453041/batch-BiDA [OR]
RewriteCond %{QUERY_STRING} ^load=/files/35356441/batch-546A
RewriteRule ^/?$ http://secondsite.com/ [L,R=301]
etc.
But if you don't care what follows the /files/ part, just match the numebrs:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^load=/files/[0-9]+/batch-[0-9A-Za-z]+
RewriteRule ^/?$ http://secondsite.com/ [L,R=301]

301 Redirect all pages including query strings to main page of new domain

I am trying to 301 redirect all urls, including urls with query strings, to one single page on a new domain.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com$ [NC]
RewriteRule ^(.*)$ "http://www.domain2.com/" [R=301,L]
The problem is that this does not strip querystrings from the URL.
It redirects
domain1.com/?PageAction=VIEWPROD&ProdID=57&HS=1
to
domain2.com/?PageAction=VIEWPROD&ProdID=57&HS=1
and not
domain2.com/
Use this rule for stripping query string from resulting URL:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com$ [NC]
RewriteRule ^ http://www.domain2.com/? [R=301,L]
Note ? in the end of http://www.domain2.com/? to strip out any existing query string in the original URL.
Reference: Apache mod_rewrite Introduction

Categories