只有在设置了特定的查询字符串时,我才希望将一组url重写到不同的域。只有主域url需要重写,url路径的其余部分需要保持不变,查询字符串也需要删除。
示例:
http://www.domain-a.com/post-type-a/post-title/?template=custom
应重定向到:
http://www.domain-b.com/post-type-a/post-title/
和
http://www.domain-a.com/post-type-b/post-title/?template=custom
应重定向到:
http://www.domain-b.com/post-type-b/post-title/
和
http://www.domain-a.com/post-type-c/post-title/?template=custom
应重定向到:
http://www.domain-b.com/post-type-c/post-title/
等。
查询字符串被设置为加载不同的单个帖子模板,这样我就可以在我的编辑wordpress网站中创建一个微型站点。然而,我想使用我的另一个域名这个微站点,因此我的问题。
更新
如果我将这些行放在域-b的根目录下的.htaccess中:
RewriteCond %{HTTP_HOST} domain-a\.com$ [NC]
Rewritecond %{QUERY_STRING} ^template=custom$ [NC]
RewriteRule ^ http://www.domain-b.com%{REQUEST_URI}? [R=301,L]
重定向实际上是有效的,但是我遇到了两个问题:
1)没有加载正确的模板,因为重写规则在函数检测查询字符串之前已经删除了它。
2)由于其他重写规则,http://www.domain-b.com更改回www.doma-a.com。
所以我认为我必须在我的wordpress函数中而不是在.htaccess中找到问题1的解决方案。让我更好地解释一下我的情况:
我的主要域名与网络空间是注册在域名-b。我的wordpress网站安装在文件夹博客。我使用域-一个作为wordpress url,所以重定向到域-一个。这很好,但是如果查询字符串设置为template=custom,我想对这些wordpress重写规则做一个例外。在我的.htaccess文件中,在文件夹博客的根目录中,我有以下规则(由wordpress生成):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
这是我用来加载不同的单个post模板文件的函数:
// Add Query var
add_filter('query_vars', 'template_query_vars');
function template_query_vars( $query_vars ){
$query_vars[] = 'template';
return $query_vars;
}
// Switch template according to query parameter
add_filter( 'template_redirect', 'sjc_template' );
function sjc_template(){
global $wp_query;
if( $wp_query->get( 'template' ) ):
global $post;
$posttype = get_post_type($post->ID);
include( get_template_directory() . '/single-'. $posttype .'-custom.php' );
exit();
endif;
}
所以我试着把重写规则放进去,但这不起作用(目前为止):
// Set up the rewrite
add_action( 'init', 'template_setup_rewrites' );
function template_setup_rewrites() {
add_rewrite_rule('^/custom/?', '^/?template=custom', 'top');
flush_rewrite_rules(false);
}
对于问题2,我不得不在Wordpress生成的重写规则上做一个例外,以保留域-a.
抱歉,这一切都有点模糊,但解释起来很复杂。任何帮助都是非常感谢的!
谢谢
发布于 2012-02-06 07:08:23
将以下内容添加到站点根目录中的.htaccess
文件中。
RewriteEngine on
RewriteBase /
#if on www.domain-a.com
RewriteCond %{HTTP_HOST} ^www\.domain-a\.com$ [NC]
#and qs contains template=custom
RewriteCond %{QUERY_STRING} ^template=custom$ [NC]
#redirect any request to domain-b
RewriteRule ^ http://www.domain-b.com%{REQUEST_URI}? [R=301,L]
唯一的主要更改是更改^$
,它只将主页匹配为^
,这将与任何请求匹配。
编辑回应评论
Options +FollowSymlinks
RewriteEngine On
#if the blog is only supposed to operate on domain-a, restrict it with thie condition
RewriteCond %{HTTP_HOST} ^www\.domain-a\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/blog/.*$
RewriteRule ^(.*)$ /blog/$1 [L]
RewriteCond %{HTTP_HOST} ^www\.domain-a\.com$ [NC]
RewriteCond %{QUERY_STRING} ^template=custom$ [NC]
RewriteRule ^ http://www.domain-b.com%{REQUEST_URI}? [R=301,L]
https://stackoverflow.com/questions/9161982
复制相似问题