Wordpress stop post redirects - wordpress

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..

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

Wordpress - Force 404 Errors in Htaccess

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]

wordpress custom htaccess rewrite not redirect

I am trying to create 'faux' pages that all link to the homepage with associated variables in the GET data. For example:
http://example.com/img/IMGNAME/comment/5678/
provide data like:
http://example.com/?image=IMGNAME&comms=5678
I have the following in my htaccess file:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^img/([^/]+)/comment/([^/]+) /?image=$1&comms=$2 [L,NS]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
the issue is that i have a post that has a slug of IMGNAME (this is how im finding the post given the GET variable on the homepage).
When I manually enter http://example.com/?image=IMGNAME&comms=5678 it all works great.
When I enter http://example.com/img/IMGNAME/comment/5678/
I am redirected to http://example.com/category/IMGNAME/ (the url address changes AND it renders the single.php template)
I am assuming index.php (or '/') then gets processed by wordpress, in which it attempts to find the post with the closest name to the content of the variable im handing it and REDIRECTS to the post?
Does anyone know how to have the entered url STAY in the address bar and allow the homepage to process the GET variables?
Do i need to create a different version of the base index.php file to have it not redirect/search for similarly named posts? I still need it to process my theme template pages with wordpress tags etc.
I think rather then doing the rewriting in .htaccess you should add new custom rewrite rule wordpress way.
function my_custom_rewrites(){
add_rewrite_rule(
'img/(.*)/comment/(.*)',
'index.php?image=$matches[1]&comms=$matches[2]',
'top' );
}
add_action( 'init', 'my_custom_rewrites' );
You can also try the tips shared here http://docs.dev4press.com/tutorial/practical/debug-wordpress-rewrite-rules-matching/ that might help you understand which Rewrite rule is matching the url and why a particular template is being rendered.
Your URL might very well be matching with other patterns of rewrite rules of Wordpress. So registering your own rule in wordpress gives you the opportunity to get it checked before the core rules, Notice the third parameter of the add_rewrite_rule which says 'top', this specify the same

.htaccess subdomain languages

Im running a WordPress site with qTranslate installed.
The URLS are currently displayed as http://domain.com/en/ but i would like to change them to
http://en.domain.com
How do i make this work? qTranslate has a built-in pre-domain mode, which should do exactly what i need. But all it does is change the links (works), when visiting the page i get a 404 error.
In the admin it says "Pre-Path and Pre-Domain mode will only work with mod_rewrite/pretty permalinks. Additional Configuration is needed for Pre-Domain mode!"
I got pre-path working and i think im using pretty permalinks (/%category%/%postname%/), but ive got no idea what kind of 'additional configuration' is needed.
My .htaccess looks like this (i think standard wp output);
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I tried a lot of different things found online, but i mostly get internal server errors or just the 404.
Thanks in advance!
If your default url starts with www (like www.domain.com) then your translated urls will look like en.www.domain.com.
But if you try to login en.domain.com, you may think that your plugin doesn't working.
Before try anything be sure that any subdomains shows your WordPress homepage. With default settings WordPress should not redirect to you anywhere.
Then set your default url without www prefix (for ex. domain.com)
And check the permalink finally
If you fail at any steps, disable all plugins and even change your theme to default one.
You'd have to add wildcard domain setting at your DNS settings for the domain. https://codex.wordpress.org/Configuring_Wildcard_Subdomains‎
Wildcard domain redirect with WordPress See also this post.

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

Resources