block hacked indexed pages of my Wordpress - wordpress

Last month my WordPress site was hacked because of my theme, so many pages (300k ..) were indexed by Google.
The actual PHP files were removed and a WAF installed, but, here is my problem.
The indexed links are like this : https://example.com/?bkzlriry235916cdefgh, always the same pattern (random chars and numbers).
To get them removed from Google, I think a rewrite rule in Apache vHost or .htaccess can do the job to get a 404 or 410 HTTP Code.
Problem is that I can't match the question mark ? using regex, even if I escape it. So the rule is wrong and blocks all my site.
I did something like that and many other tests :
RewriteRule ^/\?(.*)$ https://%{HTTP_HOST}/ [NC]
Do you have an advice for me?

RewriteRule ^/\?(.*)$ https://%{HTTP_HOST}/ [NC]
There are several things wrong here:
The RewriteRule directive matches the URL-path only, not the query string (everything after the ?). You need a separate condition (RewriteCond directive) and match against the QUERY_STRING server variable.
The URL-path that the RewriteRule pattern matches against does not start with a slash.
This rule (if it did match) would trigger a 302 (temporary) redirect to the homepage and preserve the query string, resulting a redirect loop. As you stated, you need to issue a "410 Gone" to get these pages removed from Google.
Try the following instead at the top of the root .htaccess file. Importantly, this must go before the WordPress code block.
RewriteCond %{QUERY_STRING} ^\w+$
RewriteRule ^$ - [G]
This blocks (with a 410 Gone) any request to the homepage only (as per your example) that includes a query string consisting only of letters and numbers (ie. no actual URL parameter name=value pair).
If you are not expecting any query string on any URL then you can make it more generic:
RewriteCond %{QUERY_STRING} .
RewriteRule ^ - [G]

Related

Replace "name" parameter from query in wordpress

My new wordpress site has to receive and then redirect a GET request from outside that has a name parameter appended in the request. "Name" is a reserved parameter in wordpress so when the request is received i get a 404.
old request: www_myweb_com/old_purchase?name=Michael&lastname=Smith
new address: www_myweb_com/new_purchase?firstname=Michael&lastname=Smith
The redirect is already solved with a plugin, but the big deal is how to replace that name parameter to firstname. Can anyone help me with this?
Thank you in advance.
Find .htaccess file at the root of your Wordpress directory, open it and add this AT THE TOP (above the pre-existing Wordpress rules):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/old_purchase$
RewriteCond %{QUERY_STRING} ^name=(.*)&(.*) [NC]
RewriteRule (.*) /new_purchase?firstname=%1&%2 [NE,QSD,R,L]
</IfModule>
What this does:
Checks if the URL matches www.myweb.com/old_purchase
Finds the regex pattern ^name(.*)&(.*) among the URL query
(e.g. www.myweb.com/old_purchase?name=...&lastname=...&others.. matches this regex, and stores the name value as a variable, and the rest of the query parameters as well)
Redirects to www.myweb.com/new_purchase?firstname=%1&%2 where %1 is the name value stored earlier, and %2 are the rest of the query params
The full redirected URL then becomes www.myweb.com/new_purchase?firstname=...&lastname=...&others..
NOTE: I'm a n00b at Apache and htaccess syntax and language, so this may not be the best solution. But it works :)

Redirecting URL with paramter to top-level domain

I have been trying to search but no luck yet. Found different options about redirecting but nothing like what I'm looking for.
So currently the website URLs contain lang parameter like ?lang=en, ?lang=ru, ?lang=fi. Parameters are on the very end of the URL.
Idea is to move languages to top level domain. So basically I'm looking for a way to redirect all URLs that contain parameter ?lang=ru to top-level .ru domain. Same with other languages.
Can I do it via .htaccess or it shouldn't be done at all? Moving site to different domains should need redirection to pass the link juice and authority to new domains.
Hopefully, someone can lead me to the correct way of doing it.
Much appreciated!
If there are no other URL parameters that need to be preserved then you can do it like the following using mod_rewrite near the top of your .htaccess file:
RewriteEngine On
# Redirect "/foo?lang=xx" to "example.xx/foo"
RewriteCond %{QUERY_STRING} ^lang=([a-z]{2})$
RewriteRule ^ https://example.%1%{REQUEST_URI} [QSD,R=302,L]
The above matches any language code that consists of 2 lowercase letters. To match specific language codes then use alternation in the regex and change the RewriteCond directive to read:
RewriteCond %{QUERY_STRING} ^lang=(en|ru|fi|abc|etc)$
The QSD flag discards the original lang=xx query string from the redirect response (Apache 2.4). Otherwise this will be copied onto the target URL by default.
The %1 backreference contains the value of the lang URL parameter captured in the preceding condition. The REQUEST_URI server variable contains the full URL-path (no query string) from the request.
This does assume that the language specific TLD domains are hosted elsewhere. In other words, we do not need to check whether we are already at the required TLD domain.
Test with a 302 (temporary) redirect and only change to a 301 (permanent) redirect - if that is the intention - once you have confirmed that this works OK.
UPDATE: Any specific redirects, eg. lang=en to .com will need to appear first. For example:
# Redirect languages to .com
RewriteCond %{QUERY_STRING} ^lang=(en)$
RewriteRule ^ https://example.com%{REQUEST_URI} [QSD,R=302,L]
# All other language codes...
# Redirect "/foo?lang=xx" to "example.xx/foo"
RewriteCond %{QUERY_STRING} ^lang=([a-z]{2})$
RewriteRule ^ https://example.%1%{REQUEST_URI} [QSD,R=302,L]
Use alternation (as mentioned above) if there are more language codes. eg. (en|gb).
You should be able to do this via .hataccess. You can use the code below in your .htaccess
RewriteEngine on
RewriteCond %{REQUEST_URI} lang=en
RewriteRule ^ https//wwww.yourwebsite.ext [L,R]
I think it should work fine if you write same for all the others. DO give it a try and let me know.

htaccess: If the requested URL doesn't contain a specific string, redirect user to different domain while maintaining the slug

I have looked through quite a few different posts on here without any luck.
I have two sites (for example): exampledomain.com and mb.exampledomain.com
If a user accesses mb.exampledomain.com and the URL doesn't contain the string "retreat", then I want to redirect them to exampledomain.com while maintaining the slug.
So:
mb.exampledomain.com/retreat (or) mb.exampledomain.com/retreat_sunday should be accessible to the user as normal
mb.exampledomain.com/ (or) mb.exampledomain.com/about-us should redirect to exampledomain.com/ (or) exampledomain.com/about-us respectively.
I need to achieve this by using rules in a .htaccess file at the root of the directory.
Additionally, the htaccess rule shouldn't impact the accessibility of asset files on the mb.exampledomain.com/retreat page(s).
Thanks for any help you can provide!
Sounds pretty straight forward:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/retreat
RewriteRule ^ https://exampledomain.com%{REQUEST_URI} [QSA,R=301,END]
That catches all requests to URLs not starting with the literal string "retreat". Which is what you examples demonstrate. If you only want to check for the string itself, regardless of where in the path it occurs, then that should do what you ask:
RewriteEngine on
RewriteCond %{REQUEST_URI} !retreat
RewriteRule ^ https://exampledomain.com%{REQUEST_URI} [QSA,R=301,END]
It is a good idea to start out with a 302 temporary redirect and to only change it into a 301 permanent redirect once you are certain everything works as expected.

Add redirection with regular expression on WordPress Site from .htaccess file

I want to add redirection on the WordPress site from the .htaccess file
Current URL:
example.com/papaya-exfoliator/?uf
It will be redirected to
example.com/produit/papaya-exfoliator/
Currently, the "All In One Redirection" plugin is installed and it's working fine without regular expression.
Like if I hit example.com/papaya-exfoliator then it will redirect to example.com/produit/papaya-exfoliator/ but if there are any extra data in the URL like /?uf then the redirection is not working.
You could do something like the following at the top of your .htaccess file to redirect from /papaya-exfoliator/?uf to /produit/papaya-exfoliator/
RewriteEngine On
RewriteCond %{QUERY_STRING} ^uf$
RewriteRule ^(papaya-exfoliator)/?$ /produit/$1/ [QSD,R=302,L]
Note that the RewriteRule pattern (ie. ^(papaya-exfoliator)/?$) matches against the URL-path only. You need to use an additional condition (ie. RewriteCond directive) to match against the query string (ie. uf).
I've made the trailing slash on the source URL optional since you used both versions (with and without the trailing slash) in your question.
The QSD flag is necessary in order to remove the query string from the target URL (otherwise it is copied by default). The QSD flag requires Apache 2.4. If you are still on Apache 2.2 then you will need to append a ? to the end of the substitution string instead (to essentially append an empty query string). eg. /produit/$1/?.
The $1 backreference simply saves repetition by capturing papaya-exfoliator from the requested URL.
If you wish to redirect regardless of whether the query string is present or not then simply remove the RewriteCond directive. This then makes the rule in the WP plugin superfluous.
If you wish to redirect only when a query string is present (any query string) then change the RewriteCond directive to read:
RewriteCond %{QUERY_STRING} .
ie. It matches 1 character in the query string, so there is a query string on the requested URL.

.htaccess redirect Urls

I´m creating a new Wordpress Website based on a old one.
Both sites got an archive page but with different permalinks.
Old-Adress: old.url.com/anzeigen/anzeigen-detail/article/article-name123.html
New-Adress: new.url.com/anzeige/article-name123
Since I have 2000+ articles, I´m looking for a single redirect Rule to handle all of them at once.
I know how to do a simple redirection for a single URL but not multiple at once.
Try the following at the top of your .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old\.example\.com [NC]
RewriteRule ^anzeigen/anzeigen-detail/article/([\w-]+)\.html$ https://new.example.com/anzeige/$1 [R=301,L]
If the old URL /anzeigen/anzeigen-detail/article/article-name123.html does not exist at the new domain, then you don't necessarily need the preceding condition.
[\w-] - The dynamic part (ie. article-name123) can consist of the characters a-z, A-Z, 0-9, _ (underscore) and - (hyphen).
Test first with 302 (temporary) redirect to avoid any caching issues.

Resources