MoS MoS Tech – Help for Admins Tech Ramblings of MoS and assorted other musings

27Jun/1223

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 http://domain.com/sub as where the content you want to load is. And the resulting URL to only show http://domain.com butstill load the content in in the /sub folder. This could be modified many ways to suit the needs for the application at hand.
Filed under: Tech Leave a comment
Comments (23) Trackbacks (0)
  1. Thank You! It’s nice working.
    My Problem is Solved…:)

  2. Hey,

    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:

    mysite.com/folder1/page_one.php
    mysite.com/folder2/page_two.php
    mysite.com/folder3/page_three.php

    but I want it to look like this:

    mysite.com/page_one.php
    mysite.com/page_two.php
    mysite.com/page_three.php

    • RewriteCond %{QUERY_STRING} ^$
      RewriteRule ^mysite\.com/folder1/page_one\.php$ /mysite.com/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.

    • 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
      /public_html
      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 = ‘http://vivendishop.com’;

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

      that should do it hope that works
      for you

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

    something.com/basic/contact.php or something.com/advance/contact.php
    should look like something.com/contact

  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:

    http://sarasotamagazine.com/weddings-2/ — works fine
    http://www.sarasotamagazine.com/weddings-2/ — fails

    BTW- http://www.sarasotamagazine.com DOES work and will return: http://sarasotamagazine.com just fine.

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

    Any ideas?

  5. 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:
    http://www.example.com/#!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. Hi,

    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.

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

    For example, the address http://blog.symfony.in.ua/web
    should be displayed as http://blog.symfony.in.ua/,
      and address http://blog.symfony.in.ua/web/contact
    should appear as follows:
    http://blog.symfony.in.ua/contact

    Thank you.

  8. RewriteEngine on
    # Change example.com to be your main domain.
    RewriteCond %{HTTP_HOST} ^yourdomain.com$
    # 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 example.com 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} ^yourdomain.com$
    RewriteRule ^(/)?$ web/index.php

  9. Hi,
    You can do as above.

  10. Simple & efficient, thank you very much 😉

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

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

    now i want this kind..

    website.com/……..htm
    website.com/……..htm
    website.com/……..htm

  13. I want remove beta folder name in url
    http://linkseller.com.br/beta/login
    to
    http://linkseller.com.br/login
    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. Hi,

    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. Thanks boss for such a helpful hint.

  17. I am probably doing something wrong… I have this url http://www.abc.com which shows a temporary content. All full content has been developed in WordPress in a subfolder called /temp/. Now I want to type http://www.abc.com 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 http://www.abc.com/temp.
    What am I missing?
    Thanks!

  18. 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?


Leave a comment

No trackbacks yet.

%d bloggers like this: