Daniel Sloof
Daniel Sloof

Reputation: 12706

Send 404 when requesting index.php through .htaccess?

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?

Upvotes: 0

Views: 807

Answers (2)

Serge S.
Serge S.

Reputation: 4915

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]

Upvotes: 1

Serge S.
Serge S.

Reputation: 4915

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.

Upvotes: 2

Related Questions