Forum Topic: Redirecting non-existent files

Forum: .htaccess Forum : Redirecting • Posted by Aldo • Updated:

Hi all,

on my site I added these directives:

<IfModule mod_rewrite.c>
	RewriteEngine on
	RewriteCond %{REQUEST_URI} !^/path/to/favicon\.ico [NC]
	RewriteCond %{REQUEST_URI} favicon\.ico [NC]
	RewriteRule .* http://www.domain.tld/path/to/favicon.ico [R=301,L]
</IfModule>
<IfModule mod_rewrite.c>
	RewriteEngine on
	RewriteCond %{REQUEST_URI} !^/robots\.txt [NC]
	RewriteCond %{REQUEST_URI} robots\.txt [NC]
	RewriteRule .* http://www.domain.tld/robots.txt [R=301,L]
</IfModule>
<IfModule mod_rewrite.c>
	RewriteEngine on
	RewriteCond %{REQUEST_URI} !^/sitemap\.xml [NC]
	RewriteCond %{REQUEST_URI} sitemap\.xml [NC]
	RewriteRule .* http://www.domain.tld/sitemap.xml [R=301,L]
</IfModule>
<IfModule mod_rewrite.c>
	RewriteEngine on
	RewriteCond %{REQUEST_URI} !^/humans\.txt [NC]
	RewriteCond %{REQUEST_URI} humans\.txt [NC]
	RewriteRule .* http://www.domain.tld/humans.txt [R=301,L]
</IfModule>

Is there a way to merge them into few lines?

2 Replies to “Redirecting non-existent files”

Leon Fernandez
Posted by Leon Fernandez •

Hi Aldo, just my opinion, but the file types mentioned are basic file types and don't need to have complex htaccess rules to control them.

By default, if somebody or something is looking for those file types they would just make a Get request to the base url.

You could also just add them in the header, footer or whatever template file of your site and all references will just direct them to the location you want.

For me, I just add the basic text files (robots|humans), favicon, apple.png and whatever in the base URL and don't even mention them in my site, it is just automatically added by modern browsers.

Jeff Starr
Posted by Jeff Starr

There may be a more streamlined way of writing those directives, but I am using basically the same thing.

@Leon: What Aldo is doing here is ensuring that all requests for the specified files reach the actual file, instead of returning an error (or worse). It helps to reduce the volume of malicious scans.