Forum Moderators: phranque

Message Too Old, No Replies

Redirect A URL with .com in Directory

         

clickfire

3:41 am on Apr 20, 2015 (gmt 0)

10+ Year Member



I've run into a situation that I have never had to deal with before - redirecting a URL that has a directory containing ".com"

I've tried the usual ways of redirecting in .htaccess but the whole site goes to a 500 error. For example:

RedirectMatch permanent ^/olddirectory.com/gallery11.htm$ http://www.example.com/new-directory/


How do I redirect something like this? I assume the .com in the directory is throwing if off. Thanks.

[edited by: Ocean10000 at 3:06 pm (utc) on Apr 20, 2015]
[edit reason] examplfied [/edit]

phranque

9:19 pm on Apr 21, 2015 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



but the whole site goes to a 500 error

did you check the apache server error log for the related error message?

lucy24

9:45 pm on Apr 21, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I assume the .com in the directory is throwing if off.

It shouldn't. But what's the RedirectMatch for? Is it just so you can use the opening anchor? Normally you only need RedirectMatch when capturing part of the request for reuse-- essentially the same as in mod_rewrite-- and here I don't see any capture. In fact your rule seems to be written only for one specific page. (You ought to escape all literal periods as \. but in this specific situation it isn't a serious error.)

Error logs will generally give some information. If you're on shared hosting-- as is implied by .htaccess-- error logs typically live in the same place as access logs. Did you mean that adding this rule leads to a 500 error for all requests all the time? Then there's something wrong with the rule.

:: detour to test site ::

If the rule is exactly as you gave it, there's nothing innately wrong with it, and it redirects as intended. The request did create one ErrorLog entry-- but that's because, of course, the target page genuinely doesn't exist on my test site ;)

clickfire

6:24 am on Apr 25, 2015 (gmt 0)

10+ Year Member



Okay, the error is gone - inadvertently added a space in one of the URLs. Fixed.

Based on your comments I have this:

Redirect 301 ^/olddirectory\.com/gallery11.htm$ http://www.example.com/new-directory/

It just goes straight here, no redirect, returns a 200: http://www.example.com/olddirectory.com/gallery11.htm

lucy24

7:52 am on Apr 25, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes, an unintended space can be lethal.

If you're using Redirect instead of RedirectMatch, you have to not escape the period, and not use anchors, because now you're not using Regular Expressions. Technically what's happening is the server tries to match the literal text
^/olddirectory\.com etcetera
and, of course, this will always fail.

Incidentally, you should not be using mod_alias (Redirect by that name) unless your htaccess currently does not use mod_rewrite (RewriteRule) at all. If you try to use both for redirects, you'll get things executing in the wrong order.

clickfire

5:52 pm on Apr 25, 2015 (gmt 0)

10+ Year Member



Okay, that works. Major aha moment, thanks!