Reputation: 1584
I want to 301 redirect an entire website, but exclude everything in a folder called /uploads
which exists in the /root
directory.
I have googled for this, but didn't come up with anything, or I didn't think what I saw was right.
Can we crack this?
Upvotes: 80
Views: 116657
Reputation: 41249
A mod-alias based solution
Redirect all except a specific folder
Add the following line to your root/.htaccess :
RedirectMatch 301 ^/((?!uploads).*)$ http://newdomain.com/$1
This will redirect all pages (excluding /uploads/*) from your old domain to the newdomain.
Upvotes: 12
Reputation: 99841
I think you want this:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/uploads/
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
If you get 500 Internal Error
then double-check that you have a space between } and ! on the second line.
Upvotes: 42
Reputation: 2856
Simple answer I just stumbled upon myself.
At the top before any other calls add the following
RewriteRule ^(uploads) - [L]
Upvotes: 73
Reputation: 655845
Try this mod_rewrite rule:
RewriteEngine on
RewriteRule !^uploads($|/) http://example.com%{REQUEST_URI} [L,R=301]
This rule does match any URL path that does not begin with either /uploads
or /uploads/
(leading /
is missing in the pattern due to the path prefix removal when used in .htaccess files) and redirects the request to the corresponding path at example.com.
Upvotes: 96