0

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.

2
  • webmasters.stackexchange.com
    – 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
3

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.

2
  • 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. Aug 8 '10 at 18:54
0

You should use

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