5

I'm trying to redirect the contents of a directory that is placed at the top level of my website, to a subdirectory one level deep. I also need to exclude two files and a folder.
Nothing I've tried so far seems to work for me, the last one being:

RewriteEngine on

#RewriteCond %{REQUEST_URI} !^exludedfile\.html$ 
#RewriteCond %{REQUEST_URI} !^exludedfile\.html$ 
#RewriteCond %{REQUEST_URI} !^/folder/exludedsubfolder/.*$


RewriteRule ^/folder(.*)$ /folder/subfolder/$1 [R=301,NC,L]

Can you tell me what I'm doing wrong this time?

  • Are you trying to redirect all of the files from the root to a directory?? Can you explain why? How you got to where you are now? There is another way. You can simply edit the Apache config file and change the directory specification. It you trying to redirect specific files, then that is completely different. Would it be a few files? Do they fit a pattern? For the record, NC is for RewriteCond and it stands for No Case. – closetnoc Sep 14 '16 at 13:43
  • @closetnoc Thank you for your comment. I'm trying to redirect most of the contents of a top-level folder (say /folder) to a subfolder (say /folder/subfolder), one level below the first one. I just needed to change the hierarchy of my website. I now have to find a way to redirect all of the contents except for one subfolder and two files. For some reason, this seems to be almost impossible for me to achieve. – user70084 Sep 14 '16 at 13:48
  • Okay. I think I get it now. Are you doing this by file name?? – closetnoc Sep 14 '16 at 13:50
  • @closetnoc Yes, I'm using the names of the folders and files that I want to address, if that's what you mean. – user70084 Sep 14 '16 at 13:52
3
RewriteRule ^/folder(.*)$ /folder/subfolder/$1 [R=301,NC,L]

As just mentioned in this other answer... in per-directory .htaccess files the RewriteRule pattern matches the URL-path less the directory-prefix, so the URL-path that is matched does not start with a slash. So, the pattern should be more like: ^folder(.*)$

Unless you have a requirement to use the root .htaccess file, I would create a .htaccess file in the directory you are redirecting from instead. So, in the /folder/.htaccess file (not the document root), try the following:

RewriteEngine On
RewriteBase /folder

RewriteCond %{REQUEST_URI} !^/folder/subfolder
RewriteCond %{REQUEST_URI} !^/folder/exclude-file1\.html$
RewriteCond %{REQUEST_URI} !^/folder/exclude-file2\.html$
RewriteCond %{REQUEST_URI} !^/folder/exclude-dir
RewriteRule (.*) subfolder/$1 [R=302,L]

The NC flag is not required here.

The first RewriteCond directive is to prevent a redirect loop, since we are redirecting to a subfolder of the folder we are redirecting from!

I've also used a 302 (temporary) redirect. When you are sure it's working OK, change to a 301 (permanent) - if that is the intention. 301s are cached by the browser so can make testing problematic.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy