.htaccess redirect when URL contains parameters - .htaccess

Yes, I searched the site and found many examples, but my .htacces skills are very limited, so NONE of examples worked for me after trying to modify them to suit my needs.
Put simply, if URL is like "index.php?/............" user should be redirect to example.com (ideally, to example.com instead of example.com/index.php) immediately.
I need this because I have an old domain that was used for forum, so I get thousands of requests like "example.com/index.php?/whatever" and all these virtual pages display the same content, which hurts my rankings badly.
My index.php file has NO parameters at all (but some other files have, so the rule shouldn't affect other files).
Thanks a lot!

Try adding these rules to the htaccess file in your document root (preferably before any other rules you have there)
RewriteEngine On
RewriteCond %{QUERY_STRING} ^/
RewriteRule ^index\.php$ /? [L,R=301]

Related

htaccess redirect files with the same name to just one file (been hacked)

I would like your help to modify my .htaccess file. My site has been hacked and the malware has created thousands of pages and has really hurt my rankings. I have deleted the pages, but I want all of those links to redirect to one 404 page.
What do I need to write in .htaccess in order to redirect all of the pages that contain a certain word?
I have tried this
RewriteCond %{QUERY_STRING} ^(theword)
RewriteRule ^$ https://example.com/404.php [R=301,L]
But it doesn't seem to work: the 404 page I created appears, but the link still remains in the browser, just that now it's like this
http://example.com/404.php?theword=blablabla
Maybe because my site is on a wordpress platform?
I want all the pages that start with ?theword= to be redirected entirely and for the new link to appear /404.php not /404.php?theword=
Thank you in advance for your help!
The [QSD] (query string discard) flag added to your RewriteRule will drop the query string.

Redirect rule in .htaccess to catch all URL's that are not in the .htaccess file

I have used Stackoverflow a lot to find answers to my questions, but this time I can not find an answer. Maybe it is because english is not my native language and therefore I can not find the right query to search.
I have created a .htaccess files for redirects from our current website to our new website on a new domain. The .htaccess file has 266 rules now and it works fine (I have tested several rules and the redirects work fine on a individual level).
Now my question: I would like to create an extra rule that catches all the remaining URL's that are not in the .htaccess file and redirect all of that URL's to the new homepage. These include pages that we will not create on the new domain because they are outdated or pages that we maybe forgot etc.
I have tried several rules like:
RewriteRule ^(.*)$ https://www.newdomain.com/ [L,R=301,NC]
But this redirects all URL's to the new homepage, so it ignores all other rules I have made in de .htaccess file. Even if I put the rule in the end of the file.
I hope you guys can help! tnx in advance!
Pike
this may be not help much, but the "L" Flag tells Apache not to process any more rules if this one is used.
try
RewriteRule ^(.*)$ https://www.newdomain.com/ [R=301,NC]
and more rewrite cheatsheets in here

Redirecting to specific websites using error 301

I have two domain names bought mainly for SEO, I want to redirect them to certain pages on website of a different domain name.
I know I need to redirect them with 301's in the .htaccess.
But I'm getting confused, do I need to create essentially 3 .htaccess files one on each domain?
Thanks
No, you only need one .htaccess file per domain. However, be wary of including or excluding the www. in your URL, as you may render your SEO useless.
Redirecting to a non www. URL:
This assumes you are using Apache or something similar.
Open your existing .htaccess file using a text editor, and append the following code to it:
Options +FollowSymLinks
RewriteEngine on RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^example\.com
RewriteRule (.*) http://example.com/$1
[R=301,L]
Where example.com/ is your domain. As far as I am aware, you can append any page to this URL with the same effect.
Redirecting to a www. URL:
Again, assuming Apache or similar:
Open your .htaccess file in the text editor, and append:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1
[R=301,L]
Again, example.com/ refers to your own domain. Again, AFAIK you can append specific pages without issue.
With most websites, www.example.com and example.com will take you to the same place. If you haven't made any changes to this, I would advise using the former example.
Additionally, if you are using Wordpress, there are a vast number of different plugins that can handle this for you. For example (and I am not recommending this over any other plugin), https://wordpress.org/plugins/simple-301-redirects/
You may also hear the coderel="canonical" or see it from time-to-time. Some SEO professionals actually recommend this over 301, however with modern browsers and modern crawlers, this should no longer be an issue.

htaccess rewrite query string nothing works

THE PROBLEM
After looking at 50+ StackOverflow posts and trying many permutations of my htaccess file, it does nothing still.
WHAT I HAVE TRIED
Using this website to generate my htaccess file: http://www.generateit.net/mod-rewrite/
Setting AllowOverride All in my httpd.conf file and restarting Apache.
MY CURRENT HTACCESS FILE
Lives in the root directory.
RewriteEngine On
RewriteBase /
RewriteRule ^find-a-local-doctor/([^/]*)/([^/]*)$ /find-a-local-doctor/?state=$1&city=$2 [L]
WHAT I WANT TO ACCOMPLISH
Change this URL:
http://www.md1network.com/find-a-local-doctor/?state=FL&city=Tampa
To this:
http://www.md1network.com/find-a-local-doctor/FL/Tampa
ADDITIONALLY
Since the actual file doing the work is: http://www.md1network.com/find-a-local-doctor/index.php, I need to be able to parse the query string with PHP. Hopefully, I will still be able to do this to get the state and city.
Please help.
Thanks.
Your existing rule looks alright but you will need an additional external redirection rule for reverse. Put this rule before your existing rule (just below RewriteBase /).
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(find-a-local-doctor)/(?:index\.php)?\?state=([^&]+)&city=([^&\s]+) [NC]
RewriteRule ^ /%1/%2/%3? [R=301,L]

htaccess rewrite all pages to same page on another domain

So I've been looking but so far can;t find something that I'm looking for exactly. Looked at this article How can I forward ALL pages to the same exact page on a different domain using .htaccess? but it seems to only redirect my home page.
Currently, I'm using WordPress and I need to be able to forward all pages to a new domain. For example, domain1.com/about-us needs to go to domain2.com/about-us. But I have about 50 pages this needs to work on. I would like to see if there is a 1-5 line code to use for this to work.
Thanks!
Try putting this (above any wordpress rules) in the htaccess file in domain1.com's document root:
RewriteEngine On
RewriteCond %{HTTP_HOST} domain1.com [NC]
RewriteRule ^(.*)$ http://domain2.com/$1 [L,R=301]
If your 2 domains are one different webservers, or don't share a common document root, you can just use mod_alias, adding this to the htaccess file in domain1.com's document root:
Redirect 301 / http://domain2.com/

Resources