Skip to content

Instantly share code, notes, and snippets.

@fossil12
Last active December 12, 2015 05:08
Show Gist options
  • Save fossil12/4719201 to your computer and use it in GitHub Desktop.
Save fossil12/4719201 to your computer and use it in GitHub Desktop.
Apache: mod_rewrite if 404 redirect to archive and back (if 404 - no loop!)
# Redirects for 404 to www.example.com
# adds "?redirected" to url that www.example.com doesn't redirect back
# (basic idea form http://stackoverflow.com/a/11078887/286611)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) http://www.example.com/$1?redirected [R=301,L,QSA]
# Location of the error document
ErrorDocument 404 /404.html
# Redirects for 404 to archiv.example.com
# only if query "?redirected" is not in url
# (basic idea form http://stackoverflow.com/a/11078887/286611)
RewriteEngine On
RewriteCond %{QUERY_STRING} !redirected
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) http://archiv.example.com/$1 [R=301,L,QSA]
# Location of the error document
ErrorDocument 404 /404.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment