BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Redirecting from subdirectory to subdomain using .htaccess

By Chaitanya Singh | Filed Under: WordPress

I’m writing codes based on the various conditions, choose the one, which suits your need –

site – http://demo.com

sub directory – http://demo.com/mydir/

sub domain – subdom.demo.com

1) Redirection from subdir to subdomain – for All URLs
From – http://www.demo.com/mydir/{article-url}
To – http://subdom.demo.com/ {article-url}

[code autolinks=”false”]
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.demo.com
RewriteRule ^mydir/(.*)$ http://subdom.sample.com/$1 [L,R=301]
[/code]

2) Redirection from  subdir  to subdomain’s subdir
From – http://www.demo.com/mydir/{article-url}
To – http://subdom.demo.com/subdir/{article-url}

[code autolinks=”false”]
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.demo.com
RewriteRule ^mydir/(.*)$ http://subdom.sample.com/subdir/$1 [L,R=301]
[/code]

3) Redirection from subdir to subdomain 
From – http://www.demo.com/mydir/
To – http://subdom.demo.com/

[code autolinks=”false”]
# 301 redirect for main pages
RedirectMatch 301 ^/mydir/$ http://subdom.demo.com/
[/code]

4) Redirection from subdir to subdomain’s sepcific page
Lets say I want to redirect all the pages of my subdirectory to a particular page of my sub-domain, for example
http://www.demo.com/mydir/page1.php
http://www.demo.com/mydir/page2.htm
http://www.demo.com/mydir/file.htm
http://www.demo.com/mydir/abc.html

all of the above to http://subdom.demo.com/mypage.php

[code autolinks=”false”]
# permanent redirect to a particular page
RedirectMatch 301 ^/mydir/$ http://subdom.demo.com/mypage.php
[/code]

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap