Score:0

Apache, redirect for a moved directory

in flag

On our webserver a directory has been "copied" from one location to another. Now the original location is gone however I am receiving requests to provide a redirect to the new location.

I figure mod_rewrite would be the best way to do this, though I'm not above using other methods if available. The URL scheme is like this.

Old URL: www.exampleurl.com/subdir1/olddirectory/subdir2
New URL: www.exampleurl.com/subdir1/newdirectory/subdir2

Subdir1 will never change so it, and the .com portion, can probably be ignored. However the subdir2 directories are often accessed directly and there are many different options.

We already use mod_rewrite to redirect all traffic through SSL/TLS so continuing with that would probably work the best.

I feel that there is some information missing here based on the comments and answer so far. So here is how the site and directories in question are configured, on a super basic level. Hopefully this will help with further answers.

The site is spread between two VirtualHost configurations in two files, one for HTTP and one for HTTPS. The HTTP VirtualHost contains a mod_rewrite condition and rule for all traffic into it to be redirected to the HTTPS port. The directories in question are configured in separate files within conf-enabled. Both include an Alias declaration and a Directory area. The Alias is necessary as the actual files are within /mnt directories as NFS mounts.

kz flag
Where exactly are you using these directives? _Aside:_ If these directives are in the server config then you probably should _not_ be "us[ing] mod_rewrite to redirect all traffic through SSL/TLS". YMMV.
Chris Woelkers avatar
in flag
As the original directory is gone the directives would be placed in the server config.
Score:1
kz flag

Using mod_rewrite you can do something like this:

RewriteRule ^/?(subdir1)/olddirectory(?:$|/)(.*) https://www.example.com/$1/newdirectory/$2 [R=301,L]

The above will redirect as follows:

  • /subdir1/olddirectory to /subdir1/newdirectory/
  • /subdir1/olddirectory/ to /subdir1/newdirectory/
  • /subdir1/olddirectory/<something> to /subdir1/newdirectory/<something>

And redirects to HTTPS + the canonical hostname, so can be placed before your existing canonical redirect to minimise the number of redirects.

Test first with a 302 (temporary) redirect to avoid caching issues.

Chris Woelkers avatar
in flag
You mention placing the redirect before the existing canonical redirect. The existing redirect lives in the http virtual host config file and there is another virtual host config file for https. Would the new redirect be placed in one or both of those? Or perhaps in it's own file?
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.