how to set subfolder as root and restrict certain access - php

I read an article on my host on how to set a subfolder as the root of my domain, but it misled me into believing requests would all be sent to the /subfolder/ and not at all to the root, and that was not the case.
I'm using this code in .htaccess in the root and unfortunately, it's not working as desired.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subfolder/$1
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteRule ^(/)?$ subfolder/index.php [L]
What I'm trying to achieve is the following:
Root in /subfolder/
All requests to the original true root being denied as if it didn't exist at all. Such as phpinfo.php, php.ini, any page or miscellaneous item in there being denied access by the error handling set in the /subfolder/. Any request to "domain.com/whateverhere" absolutely always sent to /subfolder/.
If possible, restrict any ability to go to domain.com/subfolder/ to show a 404. I suppose a redirect is equally effective though.
I currently use a /resources/ folder in the root which is at domain.com/resources/ which contains scripts, css, images, etc. and even though the rewrite is in place to set the /subfolder/ to root, requests to domain.com/resources/ are still being accepted even though it's not in the /subfolder/. As I said before, I'd like to know how I can truly restrict any access to the true root, and force all requests to the /subfolder/.
All in all, I'd like the "root" to be completely set as the /subfolder/, have important files like phpinfo.php and php.ini in the actual root inaccessible, and still use .htaccess in the true root as usual.
I know I'm asking a lot here, so I apologize heh.
Thank you in advance to anyone who can assist.

You can add a .htpasswd file to your subfolder:
.htpasswd
admin:$apr1$EvV5M50w$9IgAqskEg5r2pIIXUzQyp0
And define it in your subfolder/.htaccess file like this:
AuthUserFile /www/site/.htpasswd
AuthType Basic
AuthName "My secret place"
require valid-user
Now opening your subfolder, you will be asked far a password and a username ( admin / admin in my example ).
And to set it as the starting folder, you can always just add
<?php header( 'Location: /subfolder/' ); die; ?>
at the very top of your index.php file, so it will always redirect you to the subfolder.

Related

User-Friendly Redirect If File Not Exist

I feel like this is a rather common request, but I am too confused about .htaccess and couldn't find a solution by Google.
I have a Laravel instance in a subdirectory of the Apache2 htdocs. Now I would like to invisibly redirect all requests from the root domain to this folder (it should be the "root" website). But the tricky thing is, this is not the only folder, there are other folders directly in the htdocs, which should be reached normally. Just the "root" website is not in the root but also in a subfolder. For example:
https://domainA.com should load https://domainA.com/laravel/public (including possible query string or parameters, but invisibly for the user)
https://domainA.com/websiteB should be served as it is
https://domainA.com/websiteC should be served as it is
...
I assume, part of this solution will be to list all the websiteB, websiteC directories in the .htaccess, would it be possible to automate this?
Thanks in advance!
You can put a .htaccess in the folder you want to custom controle but you have to create some filter condition
<IfModule mod_rewrite.c>
RewriteEngine On
## RewriteBase /foo
## conditions to tell what to redirect ie on URI
## RewriteCond %{REQUEST_URI} ^/a-folder/
## not websiteB or websiteC
RewriteCond %{REQUEST_URI} !^/websiteB/
RewriteCond %{REQUEST_URI} !^/websiteC/
## if the file does not exist call index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ my/path/to/a/script.php [L]
</IfModule>
After you have to do something special in script.php for those HTTP calls
You can also rewrite the URI and pass it again to apache but things can be complicated after...

Manage two domains pointing to one hosting with htaccess

Well, I want to know somebody me to explain, if possible, how could I manage one hosting to have two domains pointing to it.
My purpouse is to have two folders, and manage it with htaccess.
I have two parked domains in Hostinguer, now I want to know how can I make that the htaccess only allow one folder to one domain and the other folder to another domain, and take this to all the directories, because now, I can access my webpage by the two domains and I want to know how can I solve it.
My first try was this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www|)\.domain\.com$ [NC]
RewriteRule ^ - [F]
I know more or less what is every thing, there is a little bit of Regex and some instructions that says how it has to works.
Let's say something like that in PHP:
if($_SERVER['SERVER_NAME'] == "www.domain.com") //There is an regex I know
exit; //Or, in this case throw a 403 error.
But, for example, I don't know how can I apply this action to all the subdirectories, avoiding this two-three lines to every .htaccess in every folder.
And also, It didn't work as I expected, because, it throw me a 403 error if I visit it from my domain.com, but also, it throw me a 403 error if I visit it from my subdomain: http://ikillnukes.hol.es/fapi (I will leave it running all the night to prove it)
Note: I don't have access to the Apache, so, don't suggest me to make an virtualhost because I can't. ;-)
EDIT:
I have now:
domain1.com, domain2.com and sub.domain1.com
And those folders:
root
domain1 //This is created by me
sub //This is automatically created by Hostinger, and I can't change the name or the path
domain2 //This is created by me
So, there is any problem by doing what #Walf suggested to me, but I have the problem of the subdomains and the .htaccess #Walf provided to me is a little bit compilcated to understand (my fault), so, what can I should try?
So, any help to me explaining how to approach this would be fantastic!
Thanks in advance!
Your question is probably a duplicate but I can't find one that uses adjacent dirs (as I think you're describing), nor one with a solution I'd use, myself. Try this in your root .htaccess:
RewriteEngine on
# capture the original request so you never have trouble with (un)escaping
RewriteCond %{THE_REQUEST} \S+\s+(\S*)
RewriteRule ^ - [E=REQ:%1]
# ensure the domain goes to a dir with the same name (ignoring www)
# get domain
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)
# see if first dir in url doesn't match domain
RewriteCond %1 =!^(?>([^|]+)\|\1)$
# capture first dir (if any) to check against above conditions and pass through as new url if not prefixed with domain dir
RewriteRule ^[^/]* %1%{ENV:REQ} [NE,DPI,PT]
Your dir structure would then be
docroot/
domain1.com/
whatever
domain2.net/
whatever
.htaccess
You should then be able to have normal .htaccess directives in each subdir, if you wish. Ensure you use RewriteBase / in those dirs.
Re-edit That just makes your situation more like most others'.
RewriteEngine on
RewriteBase /
# capture the original request so you never have trouble with (un)escaping
RewriteCond %{THE_REQUEST} \S+\s+(\S*)
RewriteRule ^ - [E=REQ:%1]
# ensure the domain goes to the required dir
# get domain
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)
RewriteRule ^ - [E=DOM:%1]
# explicitly set dir per host
RewriteCond %{ENV:DOM} =sub.domain1.com [NC]
RewriteRule !^sub/ sub%{ENV:REQ} [NE,DPI,L]
RewriteCond %{ENV:DOM} =domain2.com [NC]
RewriteRule !^d2/ d2%{ENV:REQ} [NE,DPI,L]
# allow domain1.com to proceed to root (any other rules go below)
# rules must still exclude subdirectories for other domains, e.g.:
RewriteRule ^(?!sub/|d2/)([^/]+)/([^/.]+)$ foo.php?bar=$1&baz=$2 [NE,B,L,DPI]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^(?:sub/|d2/|index\.php$) index.php [L,DPI]
# after all other rules, emulate DirectorySlash so that Apache does not naively insert hidden directory into public URL
DirectorySlash off
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (?>.*)(?<!/) %{ENV:REQ}/ [L,DPI,R]
If you're unsure of any regex, paste it into regex101.com (with the g flag off) and look at the explanation.

Static Url of a web application

My web application url is something like http://www.example.com
Now i want that the end user will always see http://www.example.com in there
browser inseted of something like http://www.example.com/index or any thing after the / will not show to the end user
i.e http://www.example.com/abc.php?id='someid'
will display in the user browser as http://www.example.com
Thank You in advance and sorry for the bad english.....
There are several ways to do that. REST web service, URL shortening, changing the alias or creating an .htacces file.
An easy way to do that would be creating an .htaccess file in your root directory.
If you’re running Apache, you can do that by creating a redirect in your site’s .htaccess file. If you don’t already have one, just create a new file called “.htaccess” in your site’s web root.
Save this inside your .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
# Send would-be 404 requests to Craft
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
</IfModule>
That rewrite rule basically says, “If a request is coming in that doesn’t map to an existing folder or file, pass it along to index.php instead.”
source http://buildwithcraft.com/help/remove-index.php
and check this htaccess remove index.php from url

Change site root directory with php

I want to change the root of my website with php functions.
This is my website (mysite.com)
This website using (current root document) and loading from index.php like this
mysite.com /
index.php
folder1
folder2
now I want when we open (mysite.com) show index.php inside of folder1.(change the root to /folder1/ )
I can use redirect but my address will be like this mysite.com/folder1
but I want this website (mysite.com) using /folder1/ as root directory with out we want to user (mysite.com/folder1)
What should I do? What is the functions for this to use in index.php (to use folder1 as root directory)?
This is an excellent fit for .htaccess usage if you can't access the httpd.conf.
RewriteEngine on
# Change yourdomain.com to be your primary domain.
RewriteCond %{HTTP_HOST} ^(www.)?yourprimarydomain.com$
# Change 'subfolder' to be the folder you will use for your primary domain.
RewriteCond %{REQUEST_URI} !^/subfolder/
# Don't change this line.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Change 'subfolder' to be the folder you will use for your primary domain.
RewriteRule ^(.*)$ /subfolder/$1
# Change yourdomain.com to be your primary domain again.
# Change 'subfolder' to be the folder you will use for your primary domain
# followed by / then the main file for your site, index.php, index.html, etc.
RewriteCond %{HTTP_HOST} ^(www.)?yourmaindomain.com$
RewriteRule ^(/)?$ subfolder/index.php [L]
You can use your .htaccess to change the file it default loads:
# Set the startfile to something else:
DirectoryIndex folder1/index.php
Changing basic functionality is a stupid thing to do*. It's confusing, especially for people who are new to the code, or if you look back in a year or so. It will slow down progress and development in the long run. A big no go!*
*Unless you know what you are doing. Which you're not if you have to ask :)
If you are very sure you want to go through with this, you can set the DOCUMENT_ROOT to something else.
// Place this as high as possible in your code, like a config
$_SERVER['DOCUMENT_ROOT'].= '/folder1';
To provide a more professional method, you could change the document_root in the httpd.conf, and have the php $_server value set to the new value serverwide.
Some other Q&As from SO that might help:
How do I change the root directory of an apache server?
how to remove folder name from url using htaccess
If you really want use php, you can place index.php file in you root dir with this content:
<?php
header('Location: /folder1/');
?>
Althought, better way to do this, is with htaccess.

Redirect rules for 404 content using .htaccess

It's quite common for us to put in progress sites at www.domain.com/dev/ and then once the client has signed off the site to move it to the top level www.domain.com, what we like is to be able to put a .htaccess file in the top level so that once we've moved the site out of /dev if the client accidently goes to www.domain.com/dev/apage.php that they be redirected to www.domain.com/apage.php, but only if www.domain.com/dev/apage.php doesn't exist.
Sometimes the dev folder will be called various other things, and ideally we don't want to have to edit the .htaccess file to match the folder name.
Thanks for any help.
You could do something like
Edited based on comments:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule dev/(.*)$ $1 [R=301,L]
Which basically means that if the file doesn't exist - RewriteCond %{REQUEST_FILENAME} !-f - then rewrite any request to dev back to the root. You have to specify dev/ in the rewrite rule as otherwise you will get stuck in a redirect loop.
This will only work however if you are using explicit files rather than a framework with everything routed through index.php for example

Categories