2

I am trying to set up a list of redirects in my .htaccess to create a manually defined 'short url' list, whereby the short url domain ("short.com") is set up as an alias for the destination domain ("longdomain.com").

The problem I'm running into is that –as expected– this basic redirect from the root works fine:

Redirect /howto http://longdomain.com/tutorials/how-to-do-something  
Redirect /where http://longdomain.com/directions/where-is-my-car    

...but is not suitable for redirecting to another domain. However, including the domain name like this:

Redirect short.com/howto http://longdomain.com/tutorials/how-to-do-something  
Redirect short.com/where http://longdomain.com/directions/where-is-my-car  

...is not working for the domain alias. It doesn't redirect, it just loads the content (/howto and /where) from the primary domain. Content that doesn't exist there I might add, so it ends up showing nothing.

Is there any way to create a similar redirects list from a domain alias to a primary domain? Or do I need to do a Rewrite for this? And what would that look like?

EDIT - A HOPEFULLY BETTER DESCRIPTION OF THE ISSUE
I have a short and a long domain, both with the same DNS, the short domain set up as an alias for the primary.
I need to create short urls to content on the primary domain. I will do this manually as I need readable URL's, not randomly generated strings. So: short.com/snowwhite would go to long.com/stories/fairytales/snowwhiteandthesevendwarfs.

7
  • It is really confusing what you are asking mostly because I suspect you are confusing two things together. Please add to your question the problem you are trying to solve and stay away from how you are trying to solve it. That may help us to understand your question better. I suspect the answer is easier than you expect. BTW- In your example, Redirect short.com/howto, the short.com/howto portion is NOT expecting a URL/URI but a URI so that Redirect /howto is correct. The domain name is in effect assumed to be this domain.
    – closetnoc
    Dec 14, 2014 at 22:39
  • closetnoc, sorry for the confusion, I guess I was trying too hard to be complete. I've added a shorter description. Thanks!
    – Jolle
    Dec 15, 2014 at 0:32
  • Okay. Now I get it. What I am not sure of, is how your web server is handling this. You will have to have both domains short and long on the web server as separate sites. A CNAME is not a replacement for a redirect or like saying one is really the other. It only ties the domain name to an IP address. Nothing more. The rest has to be done on the web server. The web server parses the packet request header to know where to direct the request. If a domain name is not set-up, then it does not know what to do. Some people have catch-all sites for this. Do you have both sites set up?
    – closetnoc
    Dec 15, 2014 at 1:29
  • No, I don't have both sites set up: Short.com is simply an alias of long.com, from your answer I gather that that is just not going to work. I'll go and set it up to have them both be separate sites then. An .htaccess on short.com with standard redirects to long.com (Redirect 301 /marco long.com/directory/polo) should work just fine. Thanks for the help!
    – Jolle
    Dec 15, 2014 at 15:04
  • It sounds like you are on the right track now! Let us know how it goes. If you need more help, I am here along with others. We have some real experts here.
    – closetnoc
    Dec 15, 2014 at 16:04

2 Answers 2

0

The Redirect directive doesn't pay any attention to the current host name. Your intention of

Redirect short.com/howto http://longdomain.com/tutorials/how-to-do-something  

appears to be to only redirect when the domain name is "short.com". mod_alias's redirect doesn't suport this syntax. The short.com/howto argument must only be a URI path (starting with a slash).

You could solve this problem using the <if> directive

<If "%{HTTP_HOST} == 'short.com'">
    Redirect permanent /howto http://longdomain.com/tutorials/how-to-do-something  
</If>

Alternately, you could use mod_rewrite:

RewriteEngine  on
RewriteCond %{HTTP_HOST} short.com [NC]
RewriteRule ^/howto$ http://longdomain.com/tutorials/how-to-do-something [L,R=301]

I have made the redirects permanent (301) redirects in my above examples which is more SEO friendly than the default 302 temporary redirects you had been using.

1
  • Thanks very much for the explanation and the elegant solution, Stephen, this answer covers everything I was hoping for. I've implemented it just now and it works beautifully!
    – Jolle
    Dec 16, 2014 at 19:17
0

Why not set up a CNAME record from short.com pointing to longdomain.com and then, based on your examples, the incoming requests would work without any short.com .htaccess rules.

2
  • Hi Steve, thanks for the suggestion. Not my area of expertise, but I'll happily give it a try. However, if I don't need any htaccess rules, how would I define the short URL? The thing is that the short url would contain a keyword and a corresponding URL on the destination domain, I need to provide these somewhere. Basically short.com/keyword would go to long.com/path/to/some-article, and the 'keyword' will usually not match the last segment in the destination URL.
    – Jolle
    Dec 15, 2014 at 0:50
  • OK, from your question it sounded like a straight redirect from short.com/something to long.com/something and then htaccess would handle it via some rules. But perhaps that is the way to do it to solve your problem. You don't need any rules to handle short.com if you are doing the redirect since they are handled by long.com rules. This is just one way it might work, but it means that your short.com and long.com paths would have to match - that may or may not be easy.
    – Steve
    Dec 15, 2014 at 22:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.