TRiG
TRiG

Reputation: 10643

.htaccess cross-domain redirect

I'm trying to create a redirect from http://example.com/links/AAA to http://links.example.net/l/AAA (where AAA is a variable). I've got that working. The problem is that both http://example.com/links/ and http://example.com/links should redirect to http://links.example.net (without the /l/).

At the moment http://example.com/links/ redirects to http://links.example.net/l/, and example.com/links redirects to http://links.example.net/l//hsphere/local/home/username/example.com/links.

Current .htaccess:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{HTTP_HOST} (.+)$ [NC]
    RewriteRule ^(.*)$ http://links.example.net/l/$1 [R=301,L]
</IfModule>

Pseudocode:

if ($path) {
    goto(http://links.example.net/l/${path}/); // Adding the trailing slash is not necessary, but would be handy. Obviously, don't add if it already exists.
} else {
    goto(http://links.example.net/);
}

I have looked through a bunch of other .htaccess questions here (good grief there are so many), but have yet to find anything equivalent.

If necessary, I can do this a different way:

<IfModule mod_rewrite.c>
    RewriteEngine on

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)$ index.php/$1
</IfModule>

And then do the redirection in PHP, where I'm a bit more at home. But that would be (a) less efficient, (b) less fun, and (c) less educational. So I'm going to try to do this the proper way.

Upvotes: 0

Views: 2120

Answers (1)

Ulrich Palha
Ulrich Palha

Reputation: 9529

Here is one way, assuming that your .htaccess file is in the root directory of your site.

RewriteEngine on
RewriteBase /

#if /links or links/ (path empty)
RewriteCond %{REQUEST_URI} ^/links/?$ [NC]
RewriteRule ^ http://links.example.net [R=301,L]

#otherwise
RewriteRule ^links/(.*)$ http://links.example.net/l/$1 [R=301,NC,L]

Upvotes: 2

Related Questions