.htaccess redirect without colon

49 views Asked by At

I'm trying to redirect an application link, but the middle colon (:) was not added. It's probably a simple problem but I couldn't find a solution.

Currently, I have the following .htaccess:

RewriteEngine on 
RewriteRule ^(.*)$ http://ts3server://ts3.domain.com [R=301,L]

Result:

http://ts3server//ts3.domain.com
1

There are 1 answers

0
MrWhite On BEST ANSWER

From comments, it would seem the intention is to redirect to ts3server://ts3.domain.com (ts3server is the intended protocol, not http as stated in your rule).

In this instance the limitation is mod_rewrite (not Apache per se). ts3server is not a protocol that mod_rewrite understands and since the substitution string does not start with a slash it is seen as a relative URL-path and so (by default) the directory-prefix (ie. /home/domain/web/domain.co/public_html/) is added back when constructing the URL to redirect to (in the Location header).

The solution is to use a mod_alias Redirect or RedirectMatch directive instead, that doesn't "validate" the protocol.

Your rule appears to suggest that everything should be redirected to a single target (many-to-one) - which doesn't look correct, but if that is the intention then use RedirectMatch. For example:

RedirectMatch 301 ^/ ts3server://ts3.domain.com

(Should there be a trailing slash on the end of the target URL?)

Otherwise, if the intention is to redirect /something to ts3server://ts3.domain.com/something then use a Redirect directive instead, that uses simple prefix matching (automatically copying the match onto the end of the target). For example:

Redirect 301 / ts3server://ts3.domain.com/

(Note the trailing slash on the target - mandatory in this example, otherwise you will get a malformed redirect for anything but requests to the root.)

Make sure you clear your browser cache before testing and test first with a 302 (temp) redirect to avoid potential caching issues.