Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTACCESS: Change domain root to sub-directory

I'm trying to use a sub-directory as the root folder for one of my domains. Using .htaccess, I use mod_rewrite's to get the job done. Here's the code I already have:

RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteCond %{REQUEST_URI} !^/domain/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /domain/$1

RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteRule ^(/)?$ /domain/index.php [L]

This gets the job done, but not entirely. For example:

If I goto http://domain.com/, it displays index.php from inside the domain folder. If I goto http://domain.com/test/, it will display the contents (or 403) of the test folder. BUT if I goto http://domain.com/test (for use of shortcuts, or even to display the folder) I get redirected to http://domain.com/domain/test/.

That is not supposed to happen. If anything, it either does a mask from the .htaccess (if test is being used) or should just goto http://domain.com/test/. I have tried to figure out a way around this, and I cannot. So I am seeking your help! :)

Any and all help is greatly appreciated.

like image 346
Nahydrin Avatar asked May 13 '11 01:05

Nahydrin


People also ask

How do I redirect a root to a subfolder?

You can redirect all requests to a subdirectory by adding an . htaccess file to the root of your domain's directory: Visit the FTP page for instructions on how to upload. Once connected, upload (or create) a text file named .

Does htaccess work on subdirectories?

htaccess file are applied to the directory in which the . htaccess file is found, and to all subdirectories thereof. However, it is important to also remember that there may have been . htaccess files in directories higher up.

How do I redirect a root folder?

No matter where you're in the system, when executing a cd command with slash (/), it will change your current directory to the root directory. The cd command is not only used to navigate towards the root directory, but also can move to the home directory or any file/folder.


1 Answers

Try this: its a little crude, but should do what you want. There is a little bit of confusion: some of what I've tried to do will depend on your RewriteBase (you might need to remove one or more / characters).

I've basically added an initial block that specifically looks for directories within your /domain/ folder that don't end with a trailing slash and added one. Let me know if it works at all.

RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteCond %{REQUEST_URI} !^/domain/
RewriteCond /domain/%{REQUEST_URI} -d
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /domain/$1/

RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteCond %{REQUEST_URI} !^/domain/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /domain/$1

RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteRule ^(/)?$ /domain/index.php [L]
like image 63
Femi Avatar answered Sep 29 '22 22:09

Femi