Need help with .htaccess 301 redirect

Hi Folks

I need a little help putting together a 301 redirect via .htaccess. Essentially I want to redirect all files and folders into the directory /pages/ so that each respective URL simply redirects to a file of the same name but with /pages/ at the beginning. I believe I can do this by:

RedirectMatch 301 /(.*) /pages/$1

However, the only caveat is that certain URLs such as the root homepage and one directory (/forum/) need to remain exactly where they are. Any idea how I can do this?

Thanks

Jonny :slight_smile:

Sorry for the bump, but any ideas?

Thanks!

You can’t make exceptions using RedirectMatch, it’ll either work on everything or nothing.

That said, mod_rewrite can do what you’re after.

Something along the lines of


RewriteEngine On
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_URI} !^/forum [NC]
RewriteRule .? pages%{REQUEST_URI} [L,R=301]

I’d leave out the R=301 redirect though and just serve as though the files are in / , which they aren’t really (obviously).

Loopy! You’ve both forgotten the escape from the RewriteRule, i.e., !-f (assuming that pages/{whatever} is actually a file).

Regards,

DK

Yikes indeed! :confused:


RewriteEngine On
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_URI} !^/forum [NC]
RewriteCond %{REQUEST_URI} !^/pages [NC]
RewriteCond pages%{REQUEST_URI} -f
RewriteRule .? pages%{REQUEST_URI} [L,R=301]