beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Redirect subdirectory to root using .htaccess

By Chaitanya Singh | Filed Under: .htaccess

Choose the code as per your need –

1) Redirect from http://www.mysample.com/subdir/ {article-url} to http://www.mysample.com/ {article-url}

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

2) Redirect from subdomain’s subdirectory (sub.mysample.com/subdir/{article-url}) to http://www.mysample.com/{article-url}

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

3) From http://www.mysample.com/subdir/ to http://www.mysample.com/

[code autolinks=”false”]
RedirectMatch 301 ^/subdir/$ http://www.mysample.com/
[/code]

If you’re using non-www version of site then change it as-

[code autolinks=”false”]
RedirectMatch 301 ^/subdir/$ http://mysample.com/
[/code]

301 is also known as permanent redirect so the above code can also be written as –

[code autolinks=”false”]
RedirectMatch permanent ^/subdir/$ http://mysample.com/
[/code]

Temporary redirect
Above mentioned redirects are permanent, if you want temporary redirect then write it as –

[code autolinks=”false”]
RedirectMatch 302 ^/subdir/$ http://mysample.com/
[/code]

OR

[code autolinks=”false”]
RedirectMatch temp ^/subdir/$ http://mysample.com/
[/code]

These permanent and temp rules applies for all the examples shared in this tutorial.

3) From http://www.mysample.com/subdir/{article-url} to http://www.mysample.com/

[code autolinks=”false”]
# Permanent(301) redirect
RedirectMatch 301 ^/subdir/.*$ http://www.mysample.com/
[/code]

4) From http://www.mysample.com/subdir/{article-url} to http://www.mysample.com /mypage.html

[code autolinks=”false”]
RedirectMatch 301 ^/subdir/.*$ http://www.mysample.com/mypage.html
[/code]

Enjoyed this post? Try these related posts

  1. How to prevent directory listing using htaccess
  2. How to create .htaccess file in WordPress
  3. htaccess RewriteRule, mod_rewrite, RewriteCond & Redirect 301
  4. How to prevent access to .htaccess – Make it more secure
  5. Redirecting from subdirectory to subdomain using .htaccess
  6. How to change default directory page using .htaccess file

Comments

  1. Gareth says

    August 29, 2013 at 9:38 PM

    Thanks for that. It got me out of a hole when I moved a site from a subdirectory to the root.

    Reply
  2. Nino says

    May 26, 2014 at 2:20 PM

    Hi, thanks for the article. If i want to redirect only the article url, from site/subdir/article-url to site/article-url, without redirect also the wp-admin and the uploads folder?
    Thanks

    Reply

Leave a Reply Cancel reply

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

SEO

  • Write SEO Articles
  • Open Graph Meta Tags
  • SEO Friendly Images
  • SEOPressor Review
  • Auto Interlink Posts

WordPress

  • Free plugins to add Open Graph Tags
  • Free plugins to add Captcha
  • Set thumbnails automatically
  • Display related Posts WP
  • Change Post excerpt length WP

Website

  • Add Facebook like button
  • Add Google plus button
  • Add Twitter button
  • Create Free Website on blogspot

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap