Apache, redirect for a moved directory
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.
Solution 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.