{"id":604,"date":"2011-06-17T15:32:06","date_gmt":"2011-06-17T15:32:06","guid":{"rendered":"https://www.divydovy.com/?p=604"},"modified":"2020-01-23T10:30:12","modified_gmt":"2020-01-23T10:30:12","slug":"wordpress-function-to-redirect-all-incoming-domain-requests-to-the-primary-domain-variant","status":"publish","type":"post","link":"https://www.divydovy.com/2011/06/wordpress-function-to-redirect-all-incoming-domain-requests-to-the-primary-domain-variant/","title":{"rendered":"WordPress Function to Redirect All Incoming Domain Requests to the Primary Domain Variant"},"content":{"rendered":"<p>This is something that has bugged me for ages. Surely there&#8217;s a way to redirect domain.com, *.domain.com, www.domain-variant.com, etc to the primary WordPress domain, www.domain.com using a WordPress function? Something that I can simply copy and paste for all my theme development without having to worry about the eventual web hosting environment or domain host.</p>\n<p>We&#8217;re assuming here that we need to do this within WordPress because:</p>\n<ul>\n<li>The domain host doesn&#8217;t provide enough web forwarding control</li>\n<li>We&#8217;re on shared hosting where we can&#8217;t do an .htaccess redirect</li>\n<li>We want to do it within WordPress for any other reason</li>\n</ul>\n<p>I&#8217;m also assuming that all the domains and variants are already pointing at the correct server/virtual host.</p>\n<p>I put a shout out to the <a href=\"http://www.wp-brighton.org.uk/\">http://www.wp-brighton.org.uk/</a> WordUp! mailing list to see what approaches people take. My favourite answer was from <a title=\"Tom Barrett on LinkedIn\" href=\"http://www.linkedin.com/in/tcbarrett\">Tom Barrett</a>, who suggested:<br />\n<code><br />\nadd_action('setup_theme', 'check_my_host');<br />\nfunction check_my_host(){<br />\n  // Check host in _SERVER<br />\n  // Find site_url<br />\n  // Check protocol http/https<br />\n  // String together pieces to compare with host<br />\n  // Parse PATH_INFO (to deal with installs in sub directories)<br />\n  // Use wp_redirect() and exit.<br />\n}<br />\n</code></p>\n<p>Now, I&#8217;m not as clever as Tom, so my code isn&#8217;t as thorough as his, but as I don&#8217;t tend to do sites that use SSL anyway, I decided to keep things simple and ignore that!</p>\n<p>Here&#8217;s my variant, totally inspired by Tom:<br />\n<code><br />\n// Redirect all site requests to this virtual host to the siteurl address<br />\nfunction check_my_host() {<br />\n\t// Check host in $_SERVER<br />\n\t$incomingHost = 'http://' . $_SERVER['HTTP_HOST'];<br />\n\t$incomingQuery = $_SERVER['REQUEST_URI'];<br />\n\t// Find site_url<br />\n\t$primaryHost = get_bloginfo ( 'siteurl' );<br />\n\t$redirectTarget = $primaryHost . $incomingQuery;<br />\n\t// Compare incoming and primary hosts<br />\n\tif ( strtolower( $incomingHost !== $primaryHost ) ) {<br />\n\t// If they're not equal, use wp_redirect() and exit<br />\n\t\twp_redirect( $redirectTarget, 301 ); exit;<br />\n\t}<br />\n}<br />\nadd_action('template_redirect', 'check_my_host');<br />\n</code></p>\n<p>I&#8217;ve tested it and it seems to work solidly for redirecting both domain root and domain + query requests.</p>\n<p>I&#8217;ve got no doubt this can be improved (to account for http/https protocols and various other clever checks) so please do leave comments and I&#8217;ll update this function (with credits where appropriate).</p>\n","protected":false},"excerpt":{"rendered":"<p>This is something that has bugged me for ages. Surely there&#8217;s a way to redirect domain.com, *.domain.com, www.domain-variant.com, etc to the primary WordPress domain, www.domain.com using a WordPress function? Something that I can simply copy and paste for all my theme development without having to worry about the eventual web hosting environment or domain host. We&#8217;re assuming here that we need to do this within WordPress because: The domain host doesn&#8217;t provide enough web forwarding control We&#8217;re on shared hosting where we can&#8217;t do an .htaccess redirect We want to do it within WordPress for any other reason I&#8217;m also [&hellip;]</p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_uag_custom_page_level_css":"","clkt_auto_upload":"0","_clkt_publish_and_upload":false,"_jetpack_memberships_contains_paid_content":false,"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":3,"activitypub_interaction_policy_quote":"","activitypub_status":"","footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_wordproof_timestamp":false},"categories":[7],"tags":[242,245,243,83,241,244,125],"my_content_publications":[],"class_list":["post-604","type-post","status-publish","format-standard","hentry","category-web","tag-242","tag-canonical","tag-domain","tag-php","tag-redirect","tag-subdomain","tag-wordpress"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":6}},"clkt_attachments":{"featured_image":null,"inline_attachments":[],"upload_candidates":[],"max_free_size":102400,"max_allowed_size":104857600},"_clkt_upload_mode":"new_only","clkt_content_hash":"0b1ab79533a67308b73052fbc7d440661da44271aaa39a408caeaed237e35f3d","customFields":{"clkt_auto_upload":"0","activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":3,"activitypub_interaction_policy_quote":"","activitypub_status":"","footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"processed_attachments":{"total_attachments":0,"uploaded_to_arweave":0,"kept_original_urls":0,"upload_summary":{"total":0,"uploaded":0,"failed":0,"too_large":0,"reused":0}}},"paymentAddress":"8_-tV4d15kVVq9Gd-Al2MAIC3LqNtJ_KgFol71TzxPw"}