301 redirecting pagination with get variables - .htaccess

I'm trying to set 301 redirects on pages that have 'page=1' in the URL to stop duplicate content issues.
e.g.
http://www.domain.com/reviews/?page=1
to
http://www.domain.com/reviews/
I've tried all of the variations I can find and can't seem to get anything to work.
RewriteRule ^reviews(/)?page=1$ http://www.domain.com/reviews/ [L,R=301]
RewriteRule ^(.*)/?page=1 http://www.domain.com/reviews/ [R=301,L]
RewriteCond %{QUERY_STRING} page=1
RewriteRule ^reviews$ http://www.domain.com/reviews/ [R=301,L,NE]
None of these have worked. I'm not really sure what else to try.
There are multiple different sections of the site that I need to do this for:
reviews
news
videos
accessories
hardware
An overall solution to redirect all ?page=1 URLs to their relevant section would be best.

Use this code to redirect every URI with ?page=1 to one without the query parameter:
RewriteCond %{QUERY_STRING} ^page=1(&|$) [NC]
RewriteRule ^ %{REQUEST_URI}? [R=301,L]
Or else if you want to redirect ONLY /reviews URI then
RewriteCond %{QUERY_STRING} ^page=1(&|$) [NC]
RewriteRule ^(reviews)/?$ /$1? [R=301,L]

the question is already answered, i just would like to mention that your rules are not working because you didn't append a trailing ? to the new url in the rewrite rule

Related

I need find way to make correct queries Redirect 301?

I have a question about redirect query 301...
Old Link: https://example.com/about.php?lang=1
New Link: https://example.com/about
I try a lot of samples for rules for .htaccess, but can't find rules for redirect queries, if someone can help share the work query?
Sample that i'm try it:
RewriteCond %{HTTP_HOST} ^example\.com$ RewriteCond %{QUERY_STRING} ^lang\=1$
RewriteRule ^/?about\.php$ example.com/about? [L,R=301,QSD,NC]
RewriteCond %{QUERY_STRING} (^|&)lang\=1($|&)
RewriteRule ^about\.php$ nalyvky.com/about [R=301,L]
This probably is what you are looking for:
RewriteEngone on
RewriteCond %{QUERY_STRING} (^|&)lang=1(&|$)
RewriteRule ^/?about\.php$ /about [QSD,R=301,L]
It externally redirects all requests to https://example.com/about.php?lang=1 to https://example.com/about by means of a 301 http response.
Often such redirection is combined with an internal rewrite to be able to process such redirected requests:
RewriteEngone on
RewriteCond %{QUERY_STRING} (^|&)lang=1(&|$)
RewriteRule ^/?about\.php$ /about [QSD,R=301,L]
RewriteRule ^/?about/?$ /about.php?lang=1 [L]
This assumes that the http host is irrelevant for the setup, your question is a bit fuzzy in that ...

301 redirect from URL with GET-parameters to the homepage

I have an old website with Joomla 1.5. It has some strange links with GET-parameters, like this:
http://www.primavista.ru/images/stories/catalog/?rand=1186511674
http://www.primavista.ru/images/stories/catalog/?rand=145388433
http://www.primavista.ru/images/stories/catalog/?rand=1553907057
http://www.primavista.ru/images/stories/catalog/?rand=1563973527
http://www.primavista.ru/images/stories/catalog/?rand=1981273478
http://www.primavista.ru/images/stories/catalog/?rand=2139631800
http://www.primavista.ru/images/stories/catalog/?rand=366928750
http://www.primavista.ru/images/stories/catalog/?rand=524689684
http://www.primavista.ru/images/stories/catalog/?rand=569077423
http://www.primavista.ru/images/stories/catalog/?rand=573405687
http://www.primavista.ru/images/stories/catalog/?rand=879649167
I want make redirect theses links to the homepage.
I tried some different instructions in .htaccess:
RewriteCond %{QUERY_STRING} ^/images/stories/catalog/?rand=([0-9]*)$
RewriteRule ^(.*)$ https://primavista.ru/? [R=301,L]
RewriteCond %{QUERY_STRING} ^/images/stories/catalog/?rand=(.*)$
RewriteRule ^(.*)$ https://primavista.ru/? [R=301,L]
RewriteCond %{QUERY_STRING} (^|&)(rand)=[^&]+ [NC]
RewriteRule ^images/stories/catalog(/.*)?$ https://primavista.ru/? [R=301,L,NC]
But no one not working. Maybe here someone can help me with this. Thanks
This probably is what you are looking for:
RewriteEngine on
RewriteCond %{QUERY_STRING} rand=\d+
RewriteRule ^/?images/stories/catalog/?$ / [R=301,L]
It is a good idea to start out with R=302 temporary redirections and to only change that to R=301 permanent redirections one you are satisfied with everything. That prevents nasty caching issues on the client side ...
UPDATE:
Your comment below indicates that you actually ask to remove the GET parameter in the redirected request, which you never mentioned before...
You can use the additional QSD flag for that:
RewriteEngine on
RewriteCond %{QUERY_STRING} rand=\d+
RewriteRule ^/?images/stories/catalog/?$ /? [R=301,QSD,L]

.HTACCESS - Redirect All Parameter URLS (news.asp/id?=1) to same page

Hi I have many URL's from an old site like below:
news_article.asp?id=51
I want to send ALL urls no matter what the ID value to /news/
I have tried to the below but I get /news/?id=51
RewriteCond %{QUERY_STRING} ^id=51$
RewriteRule ^news_article.php /news? [NC,R,L]
Thanks!
You can use this rule as the very first rule in your site root .htaccess:
RewriteCond %{QUERY_STRING} (?:^|&)id=\d+(?:&|$) [NC]
RewriteRule ^news_article\.php$ /news/? [NC,R=302,L]

Redirect URLs from Wordpress with ?post= or ?cat= in htaccess

So I need to redirect a mass of old Wordpress short urls that begin with question marks such as:
/?post=731
Or
/?cat=73
I need them to be instead /page
I tried
RewriteRule /?cat=73$ http://www.mydomain.com/page? [R=301,L]
Didn't work
I tried
RewriteCond %{QUERY_STRING} ^cat=73$
RewriteRule ^/$ page? [L,R=301]
Didn't work.
Each old URL will have to be manually redirected to a new url. Meaning:
/?cat=73 goes to /category-73
/?cat=2 goes to /main-category
/?post=731 goes to /this-page
These are just examples.
Each old link will have a NEW link - no one size fits all.
I have about 500 URLs like this. Anyone got any good ideas?
Try this rule:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^cat=73$ [NC]
RewriteRule ^ /category-73? [L,R=301]
RewriteCond %{QUERY_STRING} ^cat=2$ [NC]
RewriteRule ^ /main-category? [L,R=301]

Specific .htaccess redirect based on query_string

I need this logic to work:
I want rewrite this string for users to see
http://mysite.com/index.php?cl=mykeystring
to
http://mysite.com/otherkey/
http://mysite.com/index.php?myvar=test&cl=mykeystring&mysecondvar=morevalue
to
http://mysite.com/otherkey/myvar=test&mysecondvar=morevalue
But when http://mysite.com/otherkey/ is written, so load
http://mysite.com/index.php?cl=mykeystring, but no redirects will be done.
Is it possible? There are no possibility to change anything in codes, but only .htaccess
This logic is nearly realized by this code:
RewriteCond %{QUERY_STRING} ^(.*?)cl=mykeystring(.*?)$ [NC]
RewriteRule ^index.php$ /otherkey/%1%2? [R,L]
RewriteRule ^otherkey/(.*?)$ /index.php?cl=mykeystring&$1
but im getting some not needed amp symbols on first rewrite rule. any solutions?
I think you can do something like this:
RewriteCond %{QUERY_STRING} cl=mykeystring [NC]
RewriteRule ^index.php /otherkey/ [QSA]
Docs here: http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

Resources