Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect 301 from a Directory to a Single File

I'm having a bit of trouble figuring out something that should be simple. I want to 301 redirect everything in a directory to one single file in a new location.

In my .htaccess, I've already tried the following...

Redirect 301 /myDir/ http://www.mydomain.com/myNewDir/index.html

and this...

Redirect 301 /myDir/ http://www.mydomain.com/myNewDir/

and this...

Redirect 301 /myDir http://www.mydomain.com/myNewDir

The problem is that each of those are simply mapping each file within /myDir/, and appending it to the end of the destination URL.

After Googling, I saw something that said to do this...

Redirect 301 ^/myDir(.*) http://www.mydomain.com/myNewDir

But that just does the same thing... it's mapping the existing file location to the end of the URL.

It was easy finding lots of ".htaccess redirect" tutorials online but they seem to only show the obvious examples like 'one-to-one file mapping' or 'one-to-one directory mapping'. These tutorials also seem to neglect explaining the various relevant file directives and how to properly use them.

This particular hosting account is garbage and also has FrontPage extensions installed. Mod-rewrite fails (breaks the whole site) yet the Redirect 301 lines are operating fine. So until I can move this new (non-FrontPage) site to a more robust hosting account, I'll need to stick with the Redirect 301 one-liner.


How can I simply use a Redirect 301 to redirect everything within /myDir/ to the same single file located at /myNewDir/index.html? (I'd prefer using just /myNewDir/ if possible). Kindly explain, in detail, the file directives used in your solution.


UPDATE:

Previously accepted answer is not working.

Example:

RedirectMatch 301 /myDir1/(.*) http://mydomain.org/newpath/myDir1/index.html

...is giving a "Too many redirects occurred trying to open" error.

This is because /myDir1/(.*) is matching anyplace within the string so if the target URL contains /myDir1/ anywhere, not just the root, it will get redirected into a nasty loop.

See my own posted answer for correct solution.

like image 266
Sparky Avatar asked Oct 15 '11 21:10

Sparky


People also ask

Can I 301 redirect from one domain to another?

A 301 redirect is a permanent redirect from one URL to another. While they can redirect site page URLs, they can also redirect from one domain to another.

Can you reverse 301?

The short answer is "yes." You can reverse a 301-redirect, even though it's technically permanent. The long answer, though, is that this change may not work the way you'd expect or hope, and it could even make your problems worse.


2 Answers

I found the answer within one of my old projects.

Redirect 301 is all wrong for this. I really wanted RedirectMatch 301 instead.

RedirectMatch 301 ^/myDir/(.*) http://www.example.com/myNewDir/

Explanation(s):

http://httpd.apache.org/docs/1.3/mod/mod_alias.html#redirectmatch

"This directive is equivalent to Redirect, but makes use of standard regular expressions, instead of simple prefix matching."

http://www.zytrax.com/tech/web/regex.htm

"The ^ (circumflex or caret) outside square brackets means look only at the beginning of the target string, for example, ^Win will not find Windows in STRING1 but ^Moz will find Mozilla."

and...

"The . (period) means any character(s) in this position, for example, ton. will find tons, tone and tonneau but not wanton because it has no following character."

and...

The * (asterisk or star) matches the preceding character 0 or more times, for example, tre* will find tree (2 times) and tread (1 time) and trough (0 times).

like image 60
Sparky Avatar answered Oct 10 '22 13:10

Sparky


Try this:

RedirectMatch 301 /myDir/.* http://www.mydomain.com/myNewDir/index.html

Reference: http://httpd.apache.org/docs/1.3/mod/mod_alias.html#redirectmatch.

As far as brackets around .* are concerned, RedirectMatch uses standard regular expressions, which means that you can capture matched characters and use them in your redirect rule refferencing them as $1, $2, etc.

In regular expressions * means any number of repetitions of the previous character. . - denotes any character. So the combination .* says that this pattern match any number of any character. Hence * . * means that this pattern will match /myDir and /myDir/, and still /myDir/test.html. So * . * can also be used

like image 24
dfsq Avatar answered Oct 10 '22 12:10

dfsq