Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What's the httaccess syntax for:

Redirect http://.../anything to http://.../blog/anything
except if anything exists (file or directory)

Please explain, don't just give the rules.

share|improve this question

closed as off topic by ChrisF, Sathya, random Aug 9 '10 at 0:41

Questions on Super User are expected to relate to computer software or computer hardware within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question. – random Aug 9 '10 at 0:42
There are so many sites that one is completely lost. It should be up to you to recategorize and not just close the thread. This is just plain laziness and unpoliteness especially as you didn't mention anything in the first place. The FAQ absolutely generic and useless. – asksuperuser Aug 9 '10 at 18:34
up vote 3 down vote accepted

You will need to have the mod_rewrite module loaded in Apache. You can then specify the rules in your .htaccess file:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /blog/$1 [L,R=301]

The RewriteCond statements check that the requested filename is not an existing file or directory. The RewriteRule uses a regexp to match the whole string, which is stored in the variable $1. The actual HTTP 301 redirect is then to /blog/$1.

share|improve this answer
I don't understand how your last rule translate my rule Redirect http://.../anything to http://.../blog/anything – asksuperuser Aug 8 '10 at 17:40
Sorry, I didn't read your question carefully enough. See the updated answer. – ksoderstrom Aug 8 '10 at 18:54

You should use

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /blog/$1 [L,R=301]
share|improve this answer