繁体   English   中英

尝试将子域重定向到主域时获取302和301

[英]Getting a 302 and 301 when trying to redirect a sub domain to the primary domain

我正在尝试分解我的子域,并使用相同的请求uri将它们全部指向我的主域。 从正面看,它似乎工作正常,但是在后台,您可以看到它进行了两次重定向,但我不知道为什么或如何解决它。 我正在尝试重定向

sub.example.com/page

example.com/page

我有一个重写删除.php。 在该规则之下,我有一条应该剥离所有子域并保持协议不变的规则。

# Mobile Redirects for pages existing on root
RewriteCond %{HTTP_HOST} ^(.+)\.example.com [NC]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTPS}:s on:(s) 
RewriteRule ^(.*)$ http%1://example.com/$1 [R=301,L]

我的网络状态为此提供了301和302状态。

俗话说

sub.example.com/page

是301重定向到

example.com/page.php

然后302重定向到

example.com/page

我是否需要对上述重写进行一些修改,以使其不包括文件扩展名,即使它不在原始请求uri中也是如此? 从子域移到主域是否自动包含文件扩展名?

编辑:如果我告诉它转到特定页面,则只能使用1 301重定向才能正常工作。...我真的不想这样对每个页面都进行重写。

# Mobile Redirects for pages existing on root
RewriteCond %{HTTP_HOST} ^(.+)\.example.com [NC]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTPS}:s on:(s) 
RewriteRule ^(.*)$ http%1://example.com/page [R=301,L]

我相信我已经找到答案了。 我让它适用于除索引之外的所有页面。 我最终将其复制并添加了另一个条件。 如果有人有更好的答案,请告诉我,但这是我选择的路线。

# Mobile Redirects for index page
RewriteCond %{HTTP_HOST} ^(.+)\.example.com [NC]
RewriteCond %{REQUEST_URI} =/
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTPS}:s on:(s) 
RewriteRule ^(.*)$ http%1://example.com/$1 [R=301,L]

# Mobile Redirects for other pages
RewriteCond %{HTTP_HOST} ^(.+)\.example.com [NC]
RewriteCond %{REQUEST_URI}  !=/
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTPS}:s on:(s) 
RewriteRule ^(.*)\.php$ http%1://example.com/$1 [R=301,L] 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM