简体   繁体   中英

htaccess: how to redirect requests to a subfolder without showing the subfolder itself in the URL

As in the object of this post, i have an hosting that makes me able to configure more than one domain using the same hosting.

My root folder is: /home/my_username/

Inside this folder, i have the /public_html (/home/my_username/public_html/) folder where i have to put my files that have to be reached from the Internet.

Those kind of hosting requires a principal domain (call it http://principal-domain.ext ) that points to /home/my_username/public_html/

All other domains will point to /home/my_username/public_html/other-domain.ext/

So, as an example, if i have three domains:

So, in the end, the FOLDER STRUCTURE is like this

/home/
- my_username/  
  - public_html/  <- Here go files for principal-domain.ext
    - second-domain-folder/
    - third-domain-folder/

WHAT IF I WANT TO INSTALL WORDPRESS FOR PRINCIPAL-DOMAIN.EXT

If i want to install WordPress for principal-domain.ext, in this moment i have to put the WordPress' files into /home/my_username/public_html/

This is really an inconvenient folder structure, as the main WordPress installation is "mixed" with the subfolders dedicated to other domains.

To make me clear, i would have a folder structure like this:

Actual Wrong UNWANTED Folder Structure

/home/
- my_username/
  - public_html/      <- **HERE GO FILES FOR PRINCIPAL-DOMAIN.EXT (WRONG!)**
    - second-domain-folder/     <- second-domain.ext folder
    - third-domain-folder/      <- third-domain.ext folder
    - wp-admin/                 <- principal-domain.ext WordPress folder
    - wp-content/               <- principal-domain.ext WordPress folder
    - wp-includes/              <- principal-domain.ext WordPress folder

Instead, what i want is a folder structure like this below

My desired folder structure

/home/
- my_username/
  - public_html/
    - principal-domain-folder/  <- principal-domain.ext folder
       - wp-admin/                <- principal-domain.ext WordPress folder
       - wp-content/              <- principal-domain.ext WordPress folder
       - wp-includes/             <- principal-domain.ext WordPress folder
    - second-domain-folder/     <- second-domain.ext folder
    - third-domain-folder/      <- third-domain.ext folder

This way i can keep more in order my folder structure.

THE PROBLEM

The problem is that with the above folder structure, to access the principal-domain.ext WordPress installation, i have to use the URL http://principal-domain.ext/principal-domain-folder and this is not a good thing.

MY QUESTION

THE ONLY THING I'D LIKE TO OBTAIN is the ability to access the WordPress installation of principal-domain.ext using the URL http://principal-domain.ext and NOT http://principal-domain.ext/principal-domain-folder .

I don't want a redirect, but a rewriting of the URL from the real path URL to the "display" URL. I want to strip the folder /principal-domain-folder/ from the URL.

How can i do this using .htaccess file?

I've read a lot of questions and relative answers, both here at StackOverflow and on other sites on the Internet but all the solutions i've tried worked as a redirect.

The very few that seemed to do what i want, didn't work (loops errors, wrong redirect, etc.).

More, they were very complex, but i think that a simple rewrite as this doesn't require 7 or more RewriteRules as in the snippets i've found around.

Is there anyone who can help me to obtain what i'm trying to?

What rules should i have to put in my .htaccess file to access WordPress using an url like http://principal-domain.ext instead of http://principal-domain/principal-domain-folder/ ?

Try these rules:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^principal-domain.ext
RewriteCond %{REQUEST_URI} !^/principal-domain-folder/
RewriteRule ^(.*)$ /principal-domain-folder/$1 [L]

I tested it and it was working for me.

  1. RewriteEngine on rule is for enabling Rewrite .
  2. RewriteCond %{HTTP_HOST} ^principal-domain.ext rule is for filtering your domain.
  3. RewriteCond %{REQUEST_URI} !^/principal-domain-folder/ rule is for denying access to principal-domain-folder .
  4. RewriteRule ^(.*)$ /principal-domain-folder/$1 [L] rule is the main rewrite rule . Notice that it has a [L] flag which means it's the last rule.

Also if you need you can add your extra rules for the other domains after these rules.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM