2012-01-23 51 views
0

我试图创建一个从http://example.com/links/AAAhttp://links.example.net/l/AAA(其中AAA是一个变量)的重定向。我有这个工作。问题是http://example.com/links/http://example.com/links应该重定向到http://links.example.net(没有/l/)。.htaccess跨域重定向

此刻http://example.com/links/重定向到http://links.example.net/l/example.com/links重定向到http://links.example.net/l//hsphere/local/home/username/example.com/links

当前.htaccess

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

伪代码:

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/); 
} 

我走过一堆其他.htaccess问题在这里看着(好伤心有这么多),但还没有找到任何等价。

如果需要,我可以做到这一点不同的方式:

<IfModule mod_rewrite.c> 
    RewriteEngine on 

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

,然后执行重定向在PHP中,在那里我是有点多在家里。但是那样会(a)效率较低,(b)乐趣较少,以及(c)教育程度较低。所以我会试着以正确的方式做到这一点。

回答

2

以下是一种方法,假设您的.htaccess文件位于您的网站的根目录中。

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] 
+0

谢谢。我会在明天早上尝试第一件事并回报给你。 – TRiG