0

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.

Chris Woelkers
  • 298
  • 2
  • 11
  • 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. – MrWhite Dec 21 '21 at 19:03
  • As the original directory is gone the directives would be placed in the server config. – Chris Woelkers Dec 22 '21 at 15:38

1 Answers1

1

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.

MrWhite
  • 12,647
  • 4
  • 29
  • 41
  • 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? – Chris Woelkers Dec 22 '21 at 15:45