I'd like to redirect an entire directory to one specific file.
March 13, 2007 8:10 PM   Subscribe

301 redirectrs in .htaccess: how do I redirect calendar/* to /calendar.html ? I've tried modifying various examples online and they result in either "internal server error" or a 404 caused by incorrectly redirecting /calendar/* to /calendar.html/* .

Two examples from the experimentation (I'm in over my head; I just want this to work):

# redirect 301 /calendar http://www.domain.com/calendar.html
# 404 due to mistake above.

# RedirectMatch 301 ^/calendar/(.*).htm$ http://www.domain.com/calendar.html [L]
# internal server error
posted by Tuwa to Computers & Internet (10 answers total) 4 users marked this as a favorite
 
Don't worry about URL matching - just redirect the entire calendar folder:
RewriteRule ^([a-z]+) /calendar.html [R,NC,L]
should do the trick, but I could be wrong.
posted by niles at 8:19 PM on March 13, 2007


Yep, just tested it, and it appears to work great.
posted by niles at 8:23 PM on March 13, 2007


Response by poster: I tried it and it didn't. I guess I should have given more detail: I'm moving away from a loathsome Outlook-based calendaring non-solution which generates a metric ton of files and can apparently only be maintained from one computer (?). In any case it's slow and cumbersome and I have all these filenames like 2006d10.htm .

Also I know next to nothing about .htaccess. Given the filenames, would the bit to add be RewriteRule ^([a-z0-9]+) /calendar.html [R,NC,L] ? And is that all that I add to the .htaccess file? My other rewrites look like redirect 301 /body.html http://www.domain.com/index.html

Also, would this be a separate .htaccess to go in the subdirectory or in root?

Thanks.
posted by Tuwa at 8:32 PM on March 13, 2007


Best answer: For that second match, I think that [L] at the end of the RedirectMatch is causing your error; that's a mod_rewrite thing and not really relevant to Redirect. Other than that, it looks, well, a bit odd but workable. It will, however, only redirect URLs that end in '.htm', which I'm not sure is right. It seems like

RedirectMatch 301 ^/calendar http://www.mydomain.com/calendar.html

is all you really need.
posted by boaz at 8:40 PM on March 13, 2007


Your second example gave an error because you can't use just a RedirectMatch. RedirectMatch can only be used as a preface to RewriteRule. You should just use that instead.

Furthermore, you should stick to specifying the entire hostname + pathname in the redirect. It violates the HTTP RFC to specify just "/calendar.html" without a hostname in the redirect.
posted by Rhomboid at 8:40 PM on March 13, 2007


Best answer: Um, on second thought, that's not right, since it'll just keep redirecting. An extra slash will stop that:

RedirectMatch 301 ^/calendar/ http://www.mydomain.com/calendar.html
posted by boaz at 8:41 PM on March 13, 2007


Oops, I meant RewriteCond where I wrote RedirectMatch. The reason RedirectMAtch gave you a server error is because it does not take the third argument (i.e. [foo]).
posted by Rhomboid at 8:42 PM on March 13, 2007


are all the extensions the same? I did something similar by doing RedirectMatch /*.php http://the/url. In my case, I wanted to redirect all the PHP stuff on a site to another location (I transfered hosting and needed to keep the old vhost up). Maybe you could do /calendar/*.htm*? RewriteRule is part of mod_rewrite, which you may or may not have (given that it didn't work, you may not).
posted by mrg at 8:45 PM on March 13, 2007


Response by poster: Thanks, everyone. Boaz, your solution & addendum worked. Niles, I can see that yours does too, so thanks for that.

Rhomboid, do you mean that the domain should be speficied on both sides of the rewrite/redirect? (e.g. "redirect 301 /body.html http://www.domain.com/index.html" should be "redirect 301 http://www.domain.com/body.html http://www.domain.com/index.html" instead?) I tried listing the domain on both sides and the redirect quit working.
posted by Tuwa at 8:58 PM on March 13, 2007


This is an easy one!

From: Ultimate htaccess Article

RedirectMatch 301 ^/calendar(.*) http://www.askapache.com/calendar.html

Or

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/calendar\.html$ [NC]
RewriteRule ^calendar.* http://askapache.com/calendar.html [R=301,NC,L]


Redirect and RedirectMatch directives are from the module mod_alias, while the rewrite* directives are from the module mod_rewrite.. Both of these will perform a 301 permanent redirect, which will update search engines. You could change the 301 to 302 for temporary.
posted by mrapache at 6:03 AM on March 14, 2007 [1 favorite]


« Older Make my aparment better!   |   Looking for a picture of a windy day Newer »
This thread is closed to new comments.