2016-10-05 34 views
1

我有一个子域(us.example.com)和主域(example.com)。我想重定向我的子域名只有一个特定图像的主域名。例如,如果我点击http://us.example.com/images/test.jpg那么它应该重定向到http://example.com/images/test.jpghtaccess重定向用于加载从主域的子域中的特定图像url

我想301下面的重定向,但它不工作。

Redirect 301 https://us.example.com/images/test.jpg https://example.com/images/test.jpg 

我已经尝试过其他规则还,但他们不是在目录中的特定图像的完整图像目录中的所有工作。

更新 更改后的规则是这样 -

RewriteCond %{HTTP_HOST} ^us\.example\.com$ [NC] RewriteRule ^/?images/test.jpg$ https://example.com/images/test.jpg [R=301,L]

,但没有运气。

+0

请贴上你的。否则htaccess文件就这个链接http://stackoverflow.com/questions/20677792/rewrite-subdomain-to-main-domain-for-images-only-using-htaccess – Kate

+1

感谢一下@Kate觉得我完全不.htaccess文件需要在这里。我已经粘贴了我申请的规则。对于你的链接,我已经提到这个,但它在完整的目录上工作,我只想为单个图像设置规则。 –

回答

1

Redirect directivemod_alias的语法如下:

Redirect [status] [URL-path] URL 

,正如你看到的,它接收/接受URL路径,而不是匹配整个URI/URL。

对于子域匹配,使用<if>指令:

<If "%{HTTP_HOST} == 'us.example.com'"> 
    RedirectPermanent /imges/test.jpg https://example.com/images/test.jpg 
</If> 

同样可使用mod_rewrite如下来实现:

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^us\.example\.com$ [NC] 
RewriteRule ^/?images/test.jpg$ https://example.com/images/test.jpg [R=301,L] 
+0

嗨@hjpotter,尝试所有,但没有工作。检查更新的问题。 –

+0

@HelloPython不工作如何?你有其他规则吗?上面的规则应该出现在你的htaccess文件的开头。 – hjpotter92

0

尝试此配合,

<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteCond %{HTTP_HOST} ^us.domain.com$ [NC] 
RewriteRule (.*) http://www.domain.com/$1 [R=301,L] 
</IfModule> 

什么将h澳鹏为上述的.htaccess是,

如果您访问

us.domain.com/somelink

它会重定向到

domain.com/somelink

编辑:

假设,如果上面没有帮助试试下面的符号链接

Options +FollowSymLinks 
RewriteEngine on 
RewriteCond %{HTTP_HOST} ^us.domain.com [NC] 
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301] 

更新1的一个:

仅供test.jpg放在

<IfModule mod_rewrite.c> 
    RewriteEngine On 
    RewriteCond %{HTTP_HOST} ^us.domain.com/images/test.jpg [NC] 
    RewriteRule (.*) http://www.domain.com/images/test.jpg [R=301,L] 
</IfModule> 
+0

谢谢!但我希望它只适用于单个图片网址,而不是所有我想要重定向的网址。 –

+0

@HelloPython你只需要test.jpg或者所有jpg文件? –

+0

@HelloPython检查上面所做的更新,并更新我是否适用。 –