Subdomains to pages

The question:

Hi I have a website on wordpress which have 2 pages which I meant to convert it to subdomain.

Eg: Let my website at be http://www.domain.com

there are two pages http://www.domain.com/page1 and http://www.domain.com/page2

I want them to be accessible at http://page1.domain.com & http://page2.domain.com respectively.

Also I have may subpages to page1 which i wanna access using subdomain of main page. I mean if http://www.domain.com/page1/subpage1 is subpage of http://www.domain.com/page1 then I want to access it as http://page1.domain.com/subpage1

I have already created subdomains in cpanel pointing them to root folder of my website and tried following htaccess.

RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} ^subsite.domain.com$ [NC]
RewriteCond %{REQUEST_URI} !^/subsite [NC]
RewriteRule ^(.*)$ /subsite/$1 [L]

I have tried different other variants of htaccess code I got from different forums and also www.stackoverflow.com

Also tried WP Subdomains (Revisited) plugin and found of no use.

Please help me to fix it.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

It’s better to use WP_Rewrite for that.

add_action('page_rewrite_rules','my_page_rules');
function my_page_rules($rules){
$pageslug = 'sample-page';
$rules['([^/]+)/?$'] = 'index.php?pagename='$pageslug'/$matches[1]';
return $rules;
}
add_filter( 'page_link', 'sub_page_link');
function sub_page_link(){
// preg_replace here
}

I have found a premium plugin which uses the same mechanism to convert pages into subdomain, it uses rewrite rules and Server WildCard Configured.

Hope it is helpful!


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment