简体   繁体   English

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

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

I'm attempting to disolve my subdomains and point them all to my primary domain with the same request uri. 我正在尝试分解我的子域,并使用相同的请求uri将它们全部指向我的主域。 On the front it looks like its working properly but behind the scenes you can see that its doing two redirects and I don't understand why or how to fix it. 从正面看,它似乎工作正常,但是在后台,您可以看到它进行了两次重定向,但我不知道为什么或如何解决它。 I'm trying to redirect 我正在尝试重定向

sub.example.com/page

to

example.com/page

I have a rewrite that strips .php. 我有一个重写删除.php。 Below that rule I have this rule that is supposed to strip all sub domains and keep the protocol the same. 在该规则之下,我有一条应该剥离所有子域并保持协议不变的规则。

# 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]

My network Status is giving me a 301 & a 302 status for this. 我的网络状态为此提供了301和302状态。

Its saying that 俗话说

sub.example.com/page

Is 301 redirecting to 是301重定向到

example.com/page.php

Then 302 redirecting to 然后302重定向到

example.com/page

Is there some modification I have to make to the above rewrite so it doesn't include the file extension, even though its not in the original request uri? 我是否需要对上述重写进行一些修改,以使其不包括文件扩展名,即使它不在原始请求uri中也是如此? Does moving from a sub domain to a primary domain automatically include file extensions? 从子域移到主域是否自动包含文件扩展名?

Edit: If I tell it to go to a specific page it works properly only using 1 301 redirect.... I really don't want to have to throw a rewrite for every page like this. 编辑:如果我告诉它转到特定页面,则只能使用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]

I believe I figured out my answer. 我相信我已经找到答案了。 I got it working for all pages besides index. 我让它适用于除索引之外的所有页面。 I ended up duplicating it and adding one more condition. 我最终将其复制并添加了另一个条件。 If someone has a better answer then please let me know but this is the route I went with. 如果有人有更好的答案,请告诉我,但这是我选择的路线。

# 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