.htaccess how to redirect a subfolder from URL ( .htaccess modification )


domain-names-sub-folder-remove

.htaccess how to redirect a subfolder from URL ( .htaccess modification )

Today I will show you how to redirect a subfolder from URL by editing or creating .htaccess file. If you are working with wordpress you already know that installation is placed into subdirectory but some people want to have WordPress URL to coincide with their website’s root (e.g. http://yourdomain.com) .

Warrning!

Be cautious while doing that because it might mess up with your plugins and don’t forget to do backup before you will start to do it.

Our goal is to hide the subfolder from URL so it won’t be visible.

Our real use case : lets say you made a wordpress website and your URL is too long because it contains yourdomainname.com/wordpress and we want to “crop” wordpress subfolder.

You have to login to you admin panel on the hosting space. Next you have to go to public_html file and locate .htaccess file then edit it ( if .htaccess file is not present you will have to create it ).

Next step for you is to add the following line of code 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.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?codingnotes.eu$
RewriteRule ^(/)?$ codingnotes [L]


Example on my website .htaccess rewrite rule to remove a subfolder from URL

Below example on my blog and how I have hidden my subfolder:

 

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

 

Another method whitch contains URL change ( part 1 ):

Create the new location for the core WordPress files to be stored ( /wordpress for example).

Go to Settings/General in your WordPress Dashboard and set the address of your main WordPress core files in WordPress address (URL): . Example: http://example.com/wordpress

In Site address (URL): set root directory’s URL. Example: http://example.com and save it.

(Do not worry about the errors that happen now! )

Now move your WordPress core files (from root directory) to the subdirectory.

Copy (NOT MOVE!) the index.php and .htaccess files from the WordPress directory into the root directory of your site (Blog address). The .htaccess file is invisible, so you may have to set your FTP client to show hidden files. If you are not using pretty permalinks, then you may not have a .htaccess file. If you are running WordPress on a Windows (IIS) server and are using pretty permalinks, you’ll have a web.config rather than a .htaccess file in your WordPress directory. For the index.php file the instructions remain the same, copy (don’t move) the index.php file to your root directory. The web.config file, must be treated differently than the .htaccess file so you must MOVE (DON’T COPY) the web.config file to your root directory.

Another method whitch contains URL change ( part 2 ):

Open your root directory’s index.php file in a text editor

Change the following and save the file. Change the line that says:
require( dirname( __FILE__ ) . '/wp-blog-header.php' );
to the following, using your directory name for the WordPress core files:
require( dirname( __FILE__ ) . '/wordpress/wp-blog-header.php' );

Login to the new location. It might now be http://example.com/wordpress/wp-admin/

If you have set up Permalinks, go to the Permalinks Screen and update your Permalink structure. WordPress will automatically update your .htaccess file if it has the appropriate file permissions. If WordPress can’t write to your .htaccess file, it will display the new rewrite rules to you, which you should manually copy into your .htaccess file (in the same directory as the main index.php file.)

Method without URL change in the link below

https://codex.wordpress.org/Giving_WordPress_Its_Own_Directory

 

Hope you learned something new

If you learned something new from this tutorial or if you have any question let me know in comments section.

Have any Question or Comment?

Leave a Reply

Your email address will not be published.

Subscribe for new posts!

Recent Posts

Recent Comments

    Blog Categories