views:

82

answers:

2

I've recently refactored an existing CodeIgniter application to use url segments instead of query strings, and I'm using a rewriterule in htaccess to rewrite stuff to index.php:

RewriteRule ^(.*)$ /index.php/$1 [L]

My problem right now is that a lot of this website's pages are indexed by google with a link to index.php. Since I made the change to use url segments instead, I don't care about these google results anymore and I want to send a 404 (no need to use 301 Move permanently, there have been enough changes, it'll just have to recrawl everything).

To get to the point: How do I redirect requests to /index.php?whatever to a 404 page? I was thinking of rewriting to a non-existent file that would cause apache to send a 404. Would this be an acceptable solution? How would the rewriterule for that look like?

edit:
Currently, existing google results will just cause the following error:

An Error Was Encountered

The URI you submitted has disallowed characters.

I tried something like:

RewriteRule ^index\.php?(.*)$ /no-exist.html [R=404,L]

But it caused an internal server error.

edit2:

CodeIgniter is already sending '400' errors, will this be sufficient to get my pages removed from google?

+2  A: 

RewriteRule's R[=code] flag allows code only from range 300-400.

Don't use redirect R flag - just try to rewrite to an unexciting page:

UPDATED: Two redirects are apposed to each other - use RewriteConds to escape the interference.

Complete .htaccess:

RewriteEngine on

RewriteCond %{REQUEST_URI} ^/index.php.*
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^(.*)$ /no-exist.html [L]

RewriteCond %{REQUEST_URI} !^/index.php.* 
RewriteCond %{REQUEST_URI} !^/no-exist.html.* 
RewriteRule ^(.*)$ /index.php/$1 [L]

Note: /no-exist.html actualy doesn't exist. Suppose, it will help you.

SergeanT
Doesn't work, still internal server error.
Daniel
I've found the problem: slash / in `/no-exists.html` statement should not be here (just `no-exists.html`). I've fixed my answer. Tested - it works.
SergeanT
That slash works fine for my other rewrite... Needless to say, that still results in an internal server error. Maybe the 2 rewriterules dont go nice together.
Daniel
Yes, slash is okay. I've fixed problems using `RewriteCond` to escape interferences. Also: RewriteRule trunkates query string, that's why pattern `^index\.php?(.*)$` doesn't work.
SergeanT
A: 

There is a special HTTP status code 410 GONE to tell the World to remove resource: The requested resource /index.php is no longer available on this server and there is no forwarding address. Please remove all references to this resource.

To send this code use [G|gone] flag in rewrite rule:

RewriteEngine on

RewriteCond %{REQUEST_URI} ^/index.php.* 
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^(.*)$ / [G,L]

RewriteCond %{REQUEST_URI} !^/index.php.* 
RewriteRule ^(.*)$ /index.php/$1 [L]
SergeanT