0

I have a file or files, on my server that I want to hide temporarily or permanently from users and search engines using .htaccess to redirect those pages to a 404 page. This means that any user or search engine robot that attempts to view the pages will instead see a 404 page.

Here are the lines I added to my .htaccess file to redirect some files to a 404 page but it doesn’t seem to work:

RedirectMatch 404 antalya_apartment.php?bid=2&page=236
RedirectMatch 404 antalya_apartment.php?bid=3&page=129

Here is the .htaccess code:

Options  +FollowSymLinks +Indexes +MultiViews

RewriteCond %{QUERY_STRING} (^|&)bid=2(&|$)
RewriteCond %{QUERY_STRING} (^|&)page=236(&|$)
RewriteCond %{QUERY_STRING} (^|&)bid=3(&|$)
RewriteCond %{QUERY_STRING} (^|&)page=129(&|$)
RewriteRule ^/antalya_apartment.php?$ http://turkish-property-world.com/ [L,R=404]
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
antalya
  • 85
  • 6
  • So what does happen when you go to `/antalya_apartment.php?bid=3&page=129`? – ChrisW May 12 '14 at 17:13
  • possible duplicate of [How to redirect URLs based on query string?](http://stackoverflow.com/questions/13073253/how-to-redirect-urls-based-on-query-string) – Giacomo1968 May 12 '14 at 17:16
  • strange with or with the pages still show up and i get an HTTP Response Header status: HTTP/1.1 200 OK. all my other htaccess RewriteRule work fine. – antalya May 12 '14 at 17:21

1 Answers1

1

Because of query strings in the URL a simple RedirectMatch won’t work. Try this instead using RewriteRule with RewriteCond:

RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)bid=2(&|$)
RewriteCond %{QUERY_STRING} (^|&)page=236(&|$)
RewriteCond %{QUERY_STRING} (^|&)bid=3(&|$)
RewriteCond %{QUERY_STRING} (^|&)page=129(&|$)
RewriteRule ^/antalya_apartment.php?$ http://your.sites.domain.name.here/ [L,R=404]

Note I set a destination of http://your.sites.domain.name.here/ since—to my knowledge—you can’t really redirect without a destination. But if there is a way of just having it drop to nothing—or the default 404—without indicating a destination I’d like to know about that.

EDIT: Also, does your Apache even parse .htaccess rules at all? Is AllowOveride set to All? Look at the official Apache documentation for more details.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
  • `(^|&)bid=[23](&|$)` would also help :) – hjpotter92 May 12 '14 at 17:45
  • this. was the sort of rule i was hunting for... but can't get it to work either. i have a RewriteCond non-www redirect to RewriteRule www in and working. even though i write the http://your.sites. as http://www, thinking it might clash it seems not to be the problem. – antalya May 12 '14 at 17:46
  • i was hoping to get both the wonderful codes to work but didn't. i quickly tried to check the reason why, if something to do with my .htaccess rules but nothing. even temporary i #the non-www to www rule and tried no luck. i changed ErrorDocument 404 /index.php back to 404.shtml and still no go. – antalya May 12 '14 at 17:59
  • 1
    @hjpotter92 Correct. But if the original poster does not understand Apache RewriteRule logic then a regex won't help them understand that. Keep it simple at first. – Giacomo1968 May 12 '14 at 17:59
  • 1
    could be one of the reasons but i know the .htaccess works fine because when i take out or add a RewriteRule, the .htaccess follows. do you think it still has to do with serverside. – antalya May 12 '14 at 18:09
  • i took everything out of the htaccess, and just left the above rule in and still no go.? – antalya May 12 '14 at 18:12
  • just got a reply from hosting. Options +ExecCGI -FollowSymLinks -Includes +IncludesNOEXEC +Indexes -MultiViews +SymLinksIfOwnerMatch AllowOverride All – antalya May 12 '14 at 18:47
  • 1
    @antalya Okay, your `.htaccess` capabilities are good. So my suggestion is to perhaps place the rule examples I have in my answer in another position in the file. Maybe near the top? Those kind of rules get processed in a specific order, so just need to ask. – Giacomo1968 May 12 '14 at 19:31
  • is at very top and not working. do we need to append with the [QSA] (query string append) tag. – antalya May 12 '14 at 20:33
  • @antalya Sorry, but without seeing your `.htaccess` file there is little more I—or anyone—can do, so best of luck solving this issue. – Giacomo1968 May 12 '14 at 20:36
  • i have stripped down the htaccess as above, and only added your code – antalya May 12 '14 at 21:05
  • 1
    @antalya Why is `RewriteEngine On` missing from your code? That is needed to enable the rules below it. – Giacomo1968 May 12 '14 at 21:09
  • @antalya Sorry, but at this point this should work and something else is a problem that you need to debug on your side. Best of luck solving this! – Giacomo1968 May 12 '14 at 21:17