Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Redirect 404 to root, return 404 code and rewrite URL in address bar

.htaccess redirects

5:04 pm on Oct 12, 2012 (gmt 0)

New User

5+ Year Member

joined:Oct 12, 2012
posts: 1
votes: 0

I'm just learning .htaccess and I've been searching online for a solution to this. I've found a number of related things and now am more confused.

Here's the situation: my client wants to do the following:

1) Redirect all 404s to the root
2) Return code 404 rather than 200 or 301
3) The final URL that appears in the address bar should read example.com, not example.com/index.html

The site use HTML. The site has PHP added as the handler for HTML files.

Can someone help me make sense of this?
8:55 pm on Oct 12, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
votes: 0

1) Very bad idea. Requests for pages that do not exist should be returned as 404 status, not 301 or 302.
2) Yes. Returning 404 is the right thing here.
3) The URL that appears in the address bar is the one that the user clicked on in the href="" part of the on-page link. URLs are defined in links. htaccess cannot change nor "make" a URL.

You can easily redirect requests for named index pages to URL ending with a trailing slash and the DirectoryIndex directive can be used to link the new request to the internal file that will serve the content. The redirect code has been posted several thousand times before in this forum.

You have the action of mod_rewrite exactly backwards. mod_rewrite cannot "make a URL for a file". Only links make URLs. mod_rewrite acts on the request only after the link is clicked. The RewriteRule alters where inside the server the content will be fetched from.