【问题标题】:htaccess redirect specific URL to different domainhtaccess 将特定的 URL 重定向到不同的域
【发布时间】:2012-08-27 07:22:12
【问题描述】:

我有 2 个域指向同一个目录,现在我想将一些 URL 重定向到一个特定的域,其余的应该在主域上。

我有一个域 www.xyz.com 这是主域站点将使用该域打开,从这个域我想将特定的 URL 重定向到不同的域,所以如果有人打开 www.xyz.com/a/theatre 然后它应该重定向到 www.abc.com/a/theatre。现在从同一个新域中,如果有人单击 URL 中没有“/a/theatre”的 URL,那么它应该重定向回主域。所以如果有人打开 www.abc.com/a/classes 那么它应该重定向到 www.xyz.com/a/classes

我使用了以下规则但不起作用,

RewriteCond %{HTTP_HOST} ^www.xyz.com$ [NC]    
RewriteCond %{REQUEST_URI} ^/a/theatre$    
RewriteRule (.*)$ http://www.abc.com/a/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^www.abc.com$ [NC]    
RewriteCond %{REQUEST_URI} !^/a/theatre    
RewriteRule (.*)$ http://www.xyz.com/a/$1 [NC,L,R=301]

感谢任何帮助。 谢谢!

这是我的 htaccess 中的所有代码,我已经在这个域上安装了 Joomla。

Options -Indexes

RewriteEngine on
RewriteBase /seo
RewriteCond %{HTTP_HOST} ^www.mcleancenter.org$ [NC]
RewriteCond %{REQUEST_URI} ^/seo/alden-theatre$
RewriteRule ^/?seo/(.*)$ http://www.aldentheatre.org/seo/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^www.aldentheatre.org$ [NC]
RewriteCond %{REQUEST_URI} !^/seo/alden-theatre
RewriteRule ^/?seo/(.*)$ http://www.mcleancenter.org/seo/$1 [NC,L,R=301]

########## Begin - Joomla! core SEF Section
#
#RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$  [NC]
RewriteRule (.*) index.php
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
#
########## End - Joomla! core SEF Section

【问题讨论】:

    标签: .htaccess mod-rewrite redirect


    【解决方案1】:

    您需要通过在规则中的正则表达式前面添加^/?a/ 来匹配前导/a/

    RewriteCond %{HTTP_HOST} ^www.xyz.com$ [NC]    
    RewriteCond %{REQUEST_URI} ^/a/theatre$    
    RewriteRule ^/?a/(.*)$ http://www.abc.com/a/$1 [R=301,L]
    
    RewriteCond %{HTTP_HOST} ^www.abc.com$ [NC]    
    RewriteCond %{REQUEST_URI} !^/a/theatre    
    RewriteRule ^/?a/(.*)$ http://www.xyz.com/a/$1 [NC,L,R=301]
    

    【讨论】:

    • 感谢乔恩的快速回复。我在两个规则中都添加了 ^/?a/,但是两个都不起作用。
    • @AmbrishDave 这些规则在不在文档根目录中的 htaccess 文件中吗?
    • 这是在 htaccess 文件中,这个文件在 "a" 文件夹中。
    • @AmbrishDave 那么不知道你做错了什么,当我将你的规则放在一个名为/a 的目录中的空白 htaccess 文件中时,一切正常。
    • @AmbrishDave 如果此文件位于名为/seo 的目录中,则您不需要规则中的/?seo/ 除此之外,它们对我来说很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-07
    • 2012-06-01
    • 2014-08-13
    • 1970-01-01
    相关资源
    最近更新 更多