htaccess rewrite rule to remove a subfolder from a URL

Using .htaccess you can do many things.  This small tidbit will focus on removing and rewriting (mask, hide) the subfolder aspect on a URL to only have the base domain present.  You will want to create a .htaccess file if one is not present already in your public_html (or main doc root) for the site.

nano -w .htaccess

Yes I like nano! got something with it? Ha.  Use whatever you like such as VIM if you prefer.  Next you will add the following text to the file.

RewriteEngine On

RewriteRule ^$ sub/

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ sub/$1

This will assume you have as where the content you want to load is. And the resulting URL to only show but still load the content in in the /sub folder. This could be modified many ways to suit the needs for the application at hand.

24 thoughts on “htaccess rewrite rule to remove a subfolder from a URL

  1. Dhirendra says:

    Thank You! It’s nice working.
    My Problem is Solved…:)

  2. Anna says:


    it works fine, but only for the content of one subfolder. How to make it work for several subfolders with several pages in each?
    I have URLs looking like this:

    but I want it to look like this:

    1. MoS says:

      RewriteCond %{QUERY_STRING} ^$
      RewriteRule ^mysite\.com/folder1/page_one\.php$ / [R=301,NE,NC,L]

      Should work for this. If each folder is a different name and also then the page.php is a different name, this would be the most direct way of setting this up. May be able to have it in a more scripted way using REGEX. But me and REGEX are not friends.

    2. Antonio says:

      The best way to do that is to go to the folder that contains all the files for your site . For example if the folder is named ” site1 ” go to it form your cpanel copy all the files to your Public_html folder

      another way to do that is once you locate the folder that has your site files and you are looking at them ,
      click on select all to select all the files or if you do not see select all
      click on the first file in the folder it should be highlighted
      then scroll down to the very last file of the folder
      press the shift button on your keyboard and click the last file
      now everything should be highlighted.
      if not repeat the process again
      until everything is highlighted .

      right click
      and click on copy

      on the little popup window on the bottom
      you should see copy file to
      make sure the folder you’re copying the files to is
      then click on the copy files button on the lower right corner of the popup window .

      all of your files should now be in the pulic_html folder
      you need to now change your configuration.php file
      right click on and click on edit once the popup window opens click on edit again
      it should open in your browser
      look for the code line that says :

      public $live_site = ‘’;

      if your domain name has a /folder
      remove it and save

      that should do it hope that works
      for you

  3. Raja Chakraborty says:

    i have same file names on different folder. Like “small”, “basic”, “advance”.
    domain name . want to show file from folder depending on condition. like this or
    should look like

  4. Looking for some tips. I have a wordpress site- but- I can rewrite to remove the www from URLs, but it causes a 401 for direct URLs. Here is an example: — works fine — fails

    BTW- DOES work and will return: just fine.

    Originally I had:
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^ [NC]
    RewriteRule ^(.*)$$1 [L,R=301]

    Any ideas?

  5. hermann says:

    Hi guys i have an issue here,need to rewrite the url of a website i’m building actually,the problem is i’m not such a good coder,i have read all stuffs online since 2days but still can’t make it.Here’s the url:!home.html i want to remove the (#,!,html) from my url as well as being able to affect other forlders of the website with this rule.

  6. Argonius says:


    I found out that this works good in most cases, but when used with Yii framework, it just does not work well.
    In case that I have installed Yii website inside a sub folder, for some reason index (default) action is always called.

    1. Hank says:

      Apticerapion for this information is over 9000-thank you!

  7. Raisa says:

    Please help set in the file .htaccess, the command prompt,
    the server URL is not displayed the folder name web.

    For example, the address
    should be displayed as,
      and address
    should appear as follows:

    Thank you.

  8. LauLv says:

    RewriteEngine on
    # Change to be your main domain.
    RewriteCond %{HTTP_HOST} ^$
    # Change ‘subdirectory’ to be the directory you will use for your main domain.
    RewriteCond %{REQUEST_URI} !^/web/
    # Don’t change the following two lines.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    # Change ‘subdirectory’ to be the directory you will use for your main domain.
    RewriteRule ^(.*)$ /web/$1
    # Change to be your main domain again.
    # Change ‘subdirectory’ to be the directory you will use for your main domain
    # followed by / then the main file for your site, index.php, index.html, etc.
    RewriteCond %{HTTP_HOST} ^$
    RewriteRule ^(/)?$ web/index.php

    1. Ryan says:

      very well done! )

  9. LauLv says:

    You can do as above.

  10. Terry says:

    Simple & efficient, thank you very much 😉

  11. Ryan says:

    Hello! thank you so much for posting this article. It really works and straight forward!

  12. magee says:

    hello . i want to change my whole website path ..that mean now im created HTML website with 10+ sub folder
    (eg:) folder1/……..htm folder2/……..htm folder3/……..htm

    now i want this kind..……..htm……..htm……..htm

  13. Kalpesh says:

    I want remove beta folder name in url
    Please help , below my .htaccess file –

    RewriteEngine On

    ## Following lines to be used if multiple domains are used to point to master domain
    ## Put your installation directory here:

    RewriteBase /beta/

    ## Do not enable rewriting for files or directories that exist
    # Protect application and system files from being viewed

    RewriteRule ^(application|core) – [F,L]

    # Allow any files or directories that exist to be displayed directly

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    Rewritecond %{REQUEST_URI} !(^/a/*$) [NC]

    ## For requests that are not actual files or directories, rewrite to index.php/URL

    RewriteRule ^(.*)$ index.php?query_string=$1 [PT,QSA,L]
    RewriteRule ^(ajax_call\.php)$ ajax_call.php/$1 [PT,QSA,L]

  14. Good info. Lucky me I ran across your site by accident (stumbleupon).
    I have saved it for later!

  15. Paul says:


    This was very useful – thank you!

    I have a single page application where the content is served from a “dist” folder, and you have a single index.html page.

    Here is my .htaccess at the moment

    RewriteEngine On
    RewriteRule ^$ dist/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ dist/$1

    All is well when you first load the application from the index page, but I get an error if I try and load it from anywhere else. I assume I need to redirect all requests to the index.html page, but not sure how to do this when I am serving content from a folder in this way.

    Hope the above makes sense! Any help much appreciated.

  16. saddaaf says:

    Thanks boss for such a helpful hint.

  17. Fernando says:

    I am probably doing something wrong… I have this url which shows a temporary content. All full content has been developed in WordPress in a subfolder called /temp/. Now I want to type and show all the content of /temp/ without showing in the url.
    I did exactly as mentioned above there, but it still shows up like
    What am I missing?

  18. Jan says:

    If I have a large site with hundreds of folders, subfolders and pages, is it possible with some generic statements to have only the main domain show in the address bar, regardless of what page or folder is accessed and displayed? If so, how?

  19. Anirban Das Sharma says:

    Hello All

    I am facing one issue.


    I have kept /d/post-name with help of below code and url works only because of this, means can’t remove:
    RewriteRule ^foldername/d/(.*)$ foldername/index.php?controller=index&task=stories_detail&slug=$1 [L]

    Is there any way we can remove the foldername from the url, and still url works.

    Please let me know asap, as website is live.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.