Wordpress - Force 404 Errors in Htaccess - wordpress

My website got hacked a few weeks ago and now I'm on the stage of removing all spammy links from my page. Some of them are 301 redirects to legit webpages of my site, which is affecting my SEO.
Example:
www.example.com/?attachment_id=93 (showing in Google results as spam) ---> www.example.com/ (legit page).
www.example.com/z (showing in Google results as spam) ---> www.example.com/zlatni-rat-... (legit page).
However, I'm not sure how to put this in .htaccess. Everytime I try something, my whole website gets a 404 error or the images don't appear, etc.
Here's what I've tried
Add this on the bottom of the file (which does not work):
ErrorDocument 404 /z
ErrorDocument 404 /?attachment_id=93**
Add the following within the <IfModule mod_rewrite.c> code tags.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
....
RewriteCond %{REQUEST_URI} !^/(?attachment_id=93|z)$ [NC]
RewriteRule ^ - [L,R=404]
</IfModule>
This makes the entire website not to work!
I appreciate any help you can provide!

ErrorDocument 404 /z
The ErrorDocument defines a custom error document, so this is indeed invalid. But this is not needed, if you just want to reject spammy/invalid URLs.
Add the following within the <IfModule mod_rewrite.c> ...
You also need to place these directives before your existing WordPress directives. Apart from being wrong, if you place them after the WordPress front controller they will never be processed anyway.
However, if you have already "fixed" your hacked site then these URLs (presumably invalid) should not be redirecting and they should already be resulting in 404s? If you are still seeing a redirect then this might be a cached redirect from your browser cache.
Anyway, in order to get Google to drop these URLs quicker from the search results then it would perhaps be better to return a 410 Gone instead. With the two examples in your question, you will need two rules in .htaccess. Something like the following:
# www.example.com/?attachment_id=93
RewriteCond %{QUERY_STRING} ^attachment_id=93$
RewriteRule ^$ - [G]
# www.example.com/z
RewriteRule ^z$ - [G]

Related

url masking via htaccess rewrite is not working

Not sure if this is the right section of Stackoverflow to ask my question...
But here it is:
So I am using the below on the .htaccess file:
RewriteEngine On
RewriteRule ^sale?$ /discount-page/
So that when people visit example.com/sale page, they see content from example.com/discount-page/
But when I visit example.com/sale it shows 404 error saying that the URL /discount-page is not available on this server...
Why is it happening?
Here's how my entire .htaccess file looks like:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Can anyone help please?
When using WordPress, you can't simply rewrite the URL in .htaccess to the %postname% (the real URL) since WP still looks at the REQUEST_URI in order to route the URL. Even though you are rewriting /sale to /discount-page/ (the actual URL), WordPress sees /sale (the requested URL) - which doesn't exist inside WP; hence the 404.
Although not your intention, you could change this to an external redirect to get around this problem (which also avoids a potential duplicate content issue). For example:
RewriteRule ^sale$ /discount-page/ [R,L]
(I removed the ? in ^sale?$, as that does look erroneous. Or do you really want to match /sale or /sal?)
Alternatively, you could try rewriting to the underlying "plain" permalink. ie. explicitly pass the %post_id%. This is different to rewriting to the %postname%, since WP shouldn't need to check the REQUEST_URI in order to route the URL. For example:
RewriteRule ^sale$ /index.php?p=123 [L]
Where 123 is the %post_id% of your discount-page. By rewriting directly to index.php, you are effectively bypassing WP's front-controller.
Note that this must go before the standard WordPress directives in .htaccess (aka the front-controller).
However, I still feel there should be a more WordPress-like way of doing this, which is why I initially suggested asking this question over on the WordPress Stack. However, if you do that, don't mention ".htaccess". What you are really creating is a URL alias or something like that. For example: Have two different URLs show the homepage

How to redirect site to 403 if a certain url get variable exists or contains an unwanted value using htaccess

In my site statistics, I'm staring to get strange URLs like these:
mysite.com/?p=casino-online-spielen
mysite.com/category/page/2/?p=casino-online-spielen
I've tried rewrites but it has messed up my WordPress rewrites or just didn't work. I am not that good in htaccess or apache.
<IfModule mod_alias.c>
RedirectMatch 403 ?p=(casino|pharmacy)
</IfModule>
What I need is a htaccess rule that sends the user to 403 error if the word casino is found, or better yet if ?p= is found in the URL, which is not supposed to happen. But I fear it will destroy archive pagination.
On the other hand, it should allows ?s= variables and whatever is in it.
Essentially follow #Starkeen suggestion and use mod_rewrite eg.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} p=(casino|pharmacy)
RewriteRule .* - [F,L]
</IfModule>
Note:
F = Forbidden = 403
L = Last = Just doooooo IT.

Fishpig Wordpress Magento - http://example.com/blog resolves to www.example.com rather than www.example.com/blog

I have a working Fishpig Wordpress Magento instance on a production server.
All the green ticks show it has succesfully configured and when visiting
http://www.example.com/blog
it works. However when I try and and visit
http://example.com/blog
it sends me back to the magento home page at www.example.com .
I have tried setting the url in wordpress to both http://example.com/blog and http://www.example.com/blog but in both situations it always sends me to the home page at site.com.
This is a concern because most experienced web users will not type www when trying to access a location.
My htaccess is as generated by Wordpress and looks like
DirectoryIndex index.html index.php
<IfModule mod_rewrite.c>
#wp generated
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Please not the wordpress site files are located at site.com/wp and it is the fishpig extension which arranges the direct from the non existent /blog path.
First of all, to start of, one way to resolve the issue is to simply make your Magento website DEFAULT to not have the www. in the url.
In Magento, go to System > Configuration > Web.
In the "Secure" and "Unsecure" tabs, find "Base URL."
The value should look something like this:
http://www.site.com/
Replace ALL references to the domain so it shows:
http://site.com/
Unfortunately, there may be a lot of appearances of it throughout the site. You'll need to do the same thing elsewhere on the server.
If you really want your site/blog to say www., though, you'll have to properly forward the .htaccess file in Magento. I'm not too keen on how Magento's .htaccess file works, but I do know how to forward a url and keep all of its additional pages as well (however, Magento's setup may interfere).
The code should look something like this:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^site.com$ [NC,OR]
RewriteRule ^(.*)$ http://www.site.com$1 [R=301,L]
Again, I don't know how well this will work with Magento's set up, but I've successfully used the above code to redirect url's to the correct url I want. That code above was actually used on a wordpress site, though.
Check for Rewrite rules in Magento's .htaccess file and try putting this above it. Be careful, though; it is VERY easy to cause 500 internal server errors if you mess up the .htaccess file, so always keep a backup.
Try this out and I hope it helps.

RewriteRule gives 404

I have troubles with mod_rewrite.
I have wordpress site located in 'wp' directory. It is accessible as http://localhost/wp/
There is page http://localhost/wp/sample-page/ on the site.
I want to make this page open with url http://localhost/wp/sample-page-2/ without redirect.
Here is what in .htaccess file:
RewriteEngine On
RewriteBase /wp/
RewriteRule ^sample-page-2/$ sample-page/ [L]
But I'm getting 404 error.
Could somebody explain me what I'm doing wrong?
Thanks in advance.
UPD:
#soju: Well, "sample-page-2" - is just example
It is not a page/post in worpress sence.
Actually I tried to add one section to the url. E.g. http://localhost/wp/sample-page/section/. And this rule RewriteRule ^sample-page/section/$ sample-page/ [L] didn't work. Then I decided to "simpliyfy" url("sample-page-2" instead of "sample-page/section") - no success.
UPD2
BTW, redirect RewriteRule ^sample-page-2/$ sample-page/ [R=301,L] works
I suppose wordpress rewrite rules are after your own rule, so you should remove [L] option to let wordpress handle sample-page url
EDIT about UPD2 : Well, it works with a 301 redirection, but you said you don't want redirection ?
You can add a filter on rewrite_rules_array to make your own rewrite rule, take a look at example on codex : http://codex.wordpress.org/Class_Reference/WP_Rewrite#Examples

Wordpress stop post redirects

I'm having a Wordpress problem regarding permalinks.
When I have a post whose permalink is say, /2009/10/podcasts, trying to access /podcasts redirects to /2009/10/podcasts. Is there any way to stop this behavior so I can handle it as a 404?
I'm using a custom 404 handler that checks if the request is a 404 error and executes a Kohana request from within Wordpress.
I just got the answer on the WP forums. It's
remove_filter('template_redirect', 'redirect_canonical');
just checked with a default installation of wordpress - and indeed it seems to be a default behaviour of wordpress to look for the path anyways - no matter what directories you add hide. So also replacing the "/2010/01/some-long-url" by "/error/some-long-url" will redirect the reuest to "/2010/01/some-long-url".
Anyways - I can still suggest two workarounds:
1) if you really want to get a real 404 error you can use the redirect method in your htaccess to forward the request to a non-existing url - just add the one redirect-line like this:
Redirect /podcasts /podcasts-error
all together the htacces could then look like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Redirect /podcasts /podcasts-error
</IfModule>
2) If you don't really need the 404 error you may also just add a page and give it the exact same url - in this case it would be "podcasts". That would de-activate the forwarding. You could then add your own custom 'error message' to that page..
Greetz, t..

Resources