简体   繁体   中英

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. 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. 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.

Its saying that

sub.example.com/page

Is 301 redirecting to

example.com/page.php

Then 302 redirecting to

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? 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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