301 redirect with GET variables - .htaccess

Good morning all,
When I do a search for the name of my site on google I end up with lots of links like mysite.com/?page=1
mysite.com/?page=2
Etc.
I would like to redirect 301 of these links which ends in mysite.com/?page=X
to monsite.com
Because I am afraid that Google will see it as duplicate content knowing that it displays all the home page of my site ...
I tried
RewriteCond %{QUERY_STRING} ^page=1(&|$) [NC]
RewriteRule ^(mysite)/?$ /$1? [R=301,L]
which doesn't work on my side.
Could you help me ?
Thanks in advance,

To redirect such requests this should be what you are looking for:
RewriteEngine on
RewriteCond %{QUERY_STRING} (?:^|&)page=\d+(?:&|$) [NC]
RewriteRule ^/?$ / [QSD,R=301,END]
Or a more general example which preserves given a path
RewriteEngine on
RewriteCond %{QUERY_STRING} (?:^|&)page=\d+(?:&|$) [NC]
RewriteRule ^ %{REQUEST_URI} [QSD,R=301,END]
Keep in mind however that even with such redirection you still have the issue that somewhere those references are generated. Google does not make them up. So to fix the actual issue and not just a symptom you will have to find the actual issue...

Related

Forced SSL in htaccess going to only the home page

I have recently added an SSL to my sites. I have added the code to the .htaccess file to force the https. The issue is that my external links that go to pages within the site are now being redirected to the homepage. The code I am using is:
RewriteEngine On
RewriteBase /
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteRule ^(.*)$ https://www.watsonelec.com%1 [R,L]
I think the issue is in the last line, as the rule is telling it to redirect to the homepage. What I can't seem to find is a rule that will say for it to go to the URL provided in the link but give it an https instead of the HTTP.
I did do a search for this topic, but all the code I found was similar to what I already had. Thank you for all your help.
Update
I have two sites I am trying to work this out for, watsonenerysolutions.com and watsonelec.com.
When I tried
RewriteOptions InheritDownBefore
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteRule ^(.*)$ https://www.watsonenergysolutions.com/$1 [R,L]
It still sent to the homepage
When I tried
RewriteOptions InheritDownBefore
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteRule ^ https://www.watsonenergysolution.com%{REQUEST_URI} [R,L]
I received an error message that said Safari can't open the page "https://www.watsonenergysolutions.com/index.php" because Safari can't find server "www.watsonenergysolutions.com"
%N backreferences are what you match in RewriteCond's. In your case, it is empty. That's why anything is going to the homepage.
You need to use $1 or %{REQUEST_URI}, both rules below are equivalent (the second may be faster because you don't -re-match unnecessarily)
RewriteRule ^(.*)$ https://www.watsonelec.com/$1 [R,L]
RewriteRule ^ https://www.watsonelec.com%{REQUEST_URI} [R,L]
Note 1: %{REQUEST_URI} value always begins with a leading /, while what you can match in a RewriteRule never begins with a leading /
Note 2: R flag uses a 302 redirect by default. Maybe you'll want to use a 301 ([R=301,L])

How to disable "?i=1" parameter in URL

My website is running on InfinityFree hosting and it ads ?i=1 suffixes (like www.mysite.com/?i=1, or /?i=2, or /?i=3) to every URL to protect websites against malicious bots, as they say.
But of course, I don't like these suffixes and want to disable them (simply redirecting www.mysite.com/anypage/?i=1 to www.mysite.com/anypage/). Note that I don't want to disable all GET parameters, but only these i=1, i=2 and i=3.
I think it could be done using .htaccess. Can someone help me, please?
Well, I've solved the problem using a code from this question.
I've just added this code in my .htaccess, and now it redirects all the URLs with "i" to the URLs without it.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)i=[^&]+(.*)$ [NC]
RewriteRule ^(.*)$ /$1?%1%2 [R=301,L]
You need to turn the RewriteEngine on first.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)i=[^&]+(.*)$ [NC]
RewriteRule ^(.*)$ /$1?%1%2 [R=301,L]

redirect with htaccess and parameters

I moved from a wordpress to a typo3 site. On my old sites, the links looked like this:
http://www.example.org/?page_id=44
now I want to redirect it to:
http://www.example.org/contact
Usually the redirects-rules are no problem, but this time I don't get it why it is not working:
I tried this:
Redirect 301 /?page_id=44 http://www.example.org/contact
as well as this:
RewriteRule http://www.example.org/\?page_id=44 http://www.example.org/contact [R=301,L]
and this here:
RewriteCond %{REQUEST_URI} ^example.org/\?page_id=44
RewriteRule ^(.*)$ http://www.example.org/contact [R=301,L]
Tried it in several browsers and incognito-mode, but it still remains wrong, it still adds the parameter instead of redirecting to the certain page.
I guess it is somehow doable with %{query_STRING} ?
Is it due to the param-thingy?
Any ideas how to solve this problem?
Okay, got it. It had something to do with the query string:
Final result:
RewriteCond %{QUERY_STRING} ^page_id=14$ [NC]
RewriteRule ^(index\.php){0,1}$ /kontakt/? [L,R=301,NC]
You can use:
RewriteCond %{THE_REQUEST} /\?page_id=44\s [NC]
RewriteRule ^/?$ /contact? [R=301,L]

301 redirecting pagination with get variables

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

How can i get my htaccess to work (subdomains)?

I'm sorta a noob at these things but I'm trying to make a simple virtual subdomain with .htaccess. I have wildcard enabled and after lots of digging, this is what I've come up with:
rewriteEngine On
rewriteCond %{HTTP_HOST} !^$
rewriteCond %{HTTP_HOST} !^(www\.)?khpedia\.com$ [NC]
rewriteCond %{HTTP_HOST}<->%{REQUEST_URI} ^(www\.)?([^.]+).*<->/([^/]+) [NC]
rewriteCond %2<->%3 !^(.*)<->\1$ [NC]
rewriteRule ^(.+) /%2/$1 [L]
My directory is setup as
-root
--wiki
----index.php
--test
Right now when I travel to wiki.khpedia.com, I get a page not found. When I travel to wiki.khpedia.com/index.php, it travels to wiki.khpedia.com/wiki/index.php. I am somehow also able to access wiki.khpedia.com/test. If it doesnt seem obvious yet, I want to be able to go to wiki.khpedia.com/index.php and see wiki.khpedia.com/wiki/index.php but not in my address bar. Sorry for the text block and thanks for the help.
This works on the Apache side:
RewriteCond %{HTTP_HOST} !^www\.khpedia\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.+).khpedia\.com$ [NC]
RewriteRule ^(.*) /%1/$1 [L,QSA]
But you're wiki software might make up it's own mind about redirects / urls.
RewriteCond ^(.*)$ /wiki/$1 [L]
I'm confused about where subdomains come into your question. Could you give some examples of URLs you want to access in the browser and what they should point to at the server?
EDIT | Ah, I see now, Wrikken's answer handles the subdomains correctly :)

Resources