1

how to redirect to 404 in .htaccess file ? Example

www.domain.com/ja/any_post or www.domain.com/ja/all_posts
www.domain.com/fr/any_post or www.domain.com/fr/all_posts

A little bit more explaining can't hurt. Example:

My original post is on URL www.domain.com/post.

I had japanese translation of it at this URL: www.domain.com/ja/post.
And also French translation: www.domain.com/fr/post.

I had to delete these translated posts and now when I type www.domain.com/ja/post to my address bar manually, URL is redirected automatically to original post at www.domain.com/post. The same goes for /fr/.

I need URL to stay www.domain.com/ja/post and show "Not found" for all posts (with /ja/ and /fr/ in URL) without using 301 to 404.php.

Here's my htaccess (WordPress):

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Another thing I forgot to point out.../ja/ and /fr/ don't really exist.

5
  • Sorry. Question is edited. Better?
    – woomi
    Commented Oct 22, 2014 at 13:47
  • If the request is already being "redirected automatically" then there must be some code in place (maybe in your CMS?) that is performing the redirect - this is not default behaviour. Removing this "redirection" should result in the default 404.
    – MrWhite
    Commented Oct 22, 2014 at 14:33
  • Maybe trouble is with canonical url redirection. Wordpress automatically tries to guess url. However, it would be nice not to turn that off completely but to add an exception.
    – woomi
    Commented Oct 22, 2014 at 14:35
  • "WordPress tries to guess the URL" - Really?! "canonical url redirection" - This sounds possible, but why would the original language page bet set as the canonical? This sounds like a WordPress issue. It should be possible to override this in .htaccess, however, I think it is better resolved in WordPress itself. (?)
    – MrWhite
    Commented Oct 22, 2014 at 14:45
  • Tried to ask this question on their support forum...almost a week passed - no single answer from them. When canonical redirection is turned off, URL stays with /ja/ (example) but the post is in English. And you can guess what's the trouble then - duplicate content.
    – woomi
    Commented Oct 22, 2014 at 14:46

2 Answers 2

1

The simplest method would be:

# Redirect to HTTP 404 File/Page Not Found
Redirect 404 "/ja/any_posts"
Redirect 404 "/ja/all_posts"
Redirect 404 "/fr/any_posts"
Redirect 404 "/fr/all_posts"

Though you could also use:

# Redirect to HTTP 404 File/Page Not Found
RewriteRule ^ja/any_posts$ - [R=404,L]
RewriteRule ^ja/all_posts$ - [R=404,L]
RewriteRule ^fr/any_posts$ - [R=404,L]
RewriteRule ^fr/all_posts$ - [R=404,L]

Or this can be written more efficiently as:

RewriteRule ^(fr|ja)/(all|any)_posts$ - [R=404,L]

Or if you meant to redirect every page in those subfolders then:

RewriteRule ^(fr|ja)/(.*?)$ - [R=404,L]

Edited following comments:

RewriteEngine On
RewriteBase /
RewriteRule ^(fr|ja)/(.*?)$ - [R=404,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
10
  • For some reason it doesn't work. :(
    – woomi
    Commented Oct 22, 2014 at 14:27
  • I think you're taking it too literly, I think the part after ja|fr/ is variable.
    – Martijn
    Commented Oct 22, 2014 at 14:38
  • Of course :D Can you please checkout htaccess I posted in question?
    – woomi
    Commented Oct 22, 2014 at 14:41
  • I think you edited the question after I had started writing my answer :) Commented Oct 22, 2014 at 14:52
  • :D Yea, maybe. Still doesn't work. :( I don't know what could be a problem. My permissions are 644, I think that's ok...
    – woomi
    Commented Oct 22, 2014 at 14:56
0

Something along these lines might do what you want. This will redirect the user to the 'main' version of the page, and add a 404 header in the process, removing the old link:

RewriteCond %{REQUEST_URI} ^/(ja|fr)/
RewriteRule ^(ja|fr)/(.*?) /$2 [L, R=404]
8
  • Same here. Doesn't work for some reason...
    – woomi
    Commented Oct 22, 2014 at 14:27
  • Maybe I'm placing code in wrong place. Ive posted my htaccess in question...
    – woomi
    Commented Oct 22, 2014 at 14:35
  • Your code rewrites it all to the index.php, and then my code fails (I test if it starts with (ja|fr), which it now doesnt). My code needs to go first
    – Martijn
    Commented Oct 22, 2014 at 14:49
  • Minor point, by specifying R=404, the user won't be "redirect[ed] to the 'main' version of the page". The substitution is ignored (should really specify - as pass through). The default 404 will be served and the URL will remain unchanged (since no redirection occurs). This would seem to be the desired result.
    – MrWhite
    Commented Oct 22, 2014 at 14:57
  • Maytbe I'm doing something wrong...still no success.
    – woomi
    Commented Oct 22, 2014 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.