enormace
enormace

Reputation: 749

How do I specify an Apache rewrite rule to catch all URLs except home page (root and index.html)

My goal is to have a 'catchall' redirect fire to a generic domain / URL when a user types in a URL such as:

http://domain.com/blah
http://domain.com/blah/blah2
http://domain.com/blah/blah2/file.pdf
http://domain.com/blah?item=1243

All of the above URLs would redirect to:

http://domain.com/about

Basically the URL could contain slashes and a query string. And I don't want the redirect to fire if the URL is:

http://domain.com

or

http://domain.com/index.html

My solution was to have an ErrorDocument 404 or ErrorDocument 500 which would redirect to http://domain.com/about but I've been told I can't do this as the page 404 page itself is being tracked (analytics etc).

Currently I have the following:

RewriteRule ^([A-Za-z0-9-]+)$ http://domain.com/about [NC,R=301]

which seems to work but not for all of the situations outlined above.

Any help would be greatly appreciated and I hope I've made myself clear.

Upvotes: 0

Views: 997

Answers (1)

Jon Lin
Jon Lin

Reputation: 143896

The problem is that your regex also matches /about, which is causing a redirect loop.

RewriteRule ^(index\.html|)$ - [L]
RewriteRule ^(?!about) %{REQUEST_URI} [L,R=301]

Or, if the only pages that you actually have is /about and /index.html, you can just use fallback resource:

FallbackResource /about

Upvotes: 1

Related Questions