Set some folder on Apache server as a defaul folder - php

I'm developing some web based application based on PHP.
I have some folder structure that will be located inside the public html file.
I'd like to make it work so that when a user types for ex. http://mysite.com/ he/she gets into http://mysite.com/public but I don't want the user to know that he/she is inside public, the user should think that his directly inside public_html folder.
Any hints?
P.S. I'm doing it on hosted server, so I have access with only Cpanel, I'm not the admin of the server.

You either need to use mod_alias or mod_rewrite for this. How much of cPanel is available to you? How much does you host let you do?
I'll just have to have a look through my WHM server to work out how to do Aliases, but you can do rewrites with a .htaccess file. I would recommend Aliases over rewrites thought, as they are less complicated and less resource-hungry.
EDIT
Just been into my root login for our WHM/cPanel based server, and I can't find any way to use mod_alias - I think this is probably because it would require an Apache restart. You will have to use mod_rewrite.
Put this in a .htaccess file in public_html:
RewriteEngine on
RewriteRule (.*) public/$1 [L]

You can set up an addon domain and point it to /public_html/public directory.
Edit:
Parked domain should work too.

Related

How to make .htaccess rewrite for language subdomain versions?

my project requires language versions and for SEO purposes I want not to create $_SESSION for language but to use subdomain name for the language version. Let say my russian version of website looks like:
ru.example.com
I want when user visits http://ru.example.com .htaccess file to load index file which is located in the root of the website..
But this is not all ! For example if user visits: http://ru.example.com/products/ .htaccess file to load index file which is located in http://example.com/products/
Is it possible this to be done somehow? Basicaly I wish subdomain name ru to be ignored somehow when server loads the file in url..
So far I have this:
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{HTTP_HOST} ^ru\.example\.com$
and I dont know how to complete the rest..
Please help if this can be done
The way to do this is simply to have all your language subdomains served by the same website. No mod_rewrite required. You can check HTTP_HOST in your php to see which is being accessed. Put them all in ServerAlias or just use a generic ServerAlias *.example.com.
Make sure to also set UseCanonicalName off so any self-referential URLs that Apache generates will use whatever host was visited and not drop your language subdomain. Reference here.
If you can't do that, let us know your hosting setup, and as long as it's on the same server it can probably be done with rewrites.

How can the document root be changed in web server behavior?

I'd like to know if there is a way of changing the relative document root for extra security. I'll try to explain myself through the following example:
/root
/app
/public
Say an www.example.com request to the web server would point to the root folder.
I was wondering if there was a configuration, for instance through an .htaccess file located in said root folder, that would make the server point to the public folder instead, therefore having any remote paths always be relative to said public folder.
In this instance, www.example.com/app would request an app folder inside of public, instead of an app folder inside of root, leaving the latter to be inaccessible from a remote url request.
In the same manner, www.example.com/public would request a public folder inside of our root public folder and so forth.
I've read various topics like this one that mention using a custom .htaccess configuration to achieve something similar, but it requires the manual configuration of the request url in said file, while my intention is for it to work without further configuration no matter where you host the application.
Another possible solution I've seen is doing a hard redirect through the .htaccess file, which does not solve anything actually.
Feel free to edit this post as I might have had a hard time trying to get my point across.
You can use this simple .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L]
</IfModule>
Any request on your server will point to the public folder.
Inside the public folder you can add an extra .htaccess file handling your site rules.
Also you can Host multiple sites on One webserver. You can combine making VirtualHosts and Alias using mod_alias mentioned before
In this example is suposed to have your own server (either dedicated or VPS)
By using Virtualhosts you can tell to the webserver when you recieve a request to www.example.com to serve content from a specific folder.
An example Virtualhost of it is:
<Virtualhost *:80>
ServerName ^domain_or_ip^
DocumentRoot ^path of the public folder^
DirectoryIndex index.php home.php index.html index.htm
ErrorLog ^path for a file containing php errors^
CustomLog ^path for logging whitch browser and ip visited your site^ combined
</Virtualhost>
I suggest that you point your server to the public folder anyhow, as it is much more secure, you could see that all frameworks behave the same way, they all have a "public" folder where the server points to.
In the public folder you have one point of entry to your scripts, like
index.php
and from this entry you will communicate with your application.
Of course you can still work the way you requested, and it will work great, but who knows maybe you will miss something and someone could access and view your "inner" files.
You're on Apache web server? If I understand correctly, you're looking for Virtual Directories. Usually, we have to put our web application inside the document root of Apache in order to make the application accessible from the network. However, there is a trick to make the web application still accessible even though we put it outside of Apache's document root. Please read up on that here: http://w3shaman.com/article/creating-virtual-directory-apache
Credits to W3Shaman.com, obviously.

Relative / Root paths on apache / cpanel / php shared hosting

I am trying to deploy a php / codeigniter project to a shared hosting environment.
Locally I am running MAMP and all my paths are referenced thus:-
background:transparent url(/img/myimj.jpg) left top no-repeat;
When I deploy the shared host, these links do not work and to resolve them I need to add "../". Changing all these references alone would be tiresome. but codeigniter paths are also affected and I want to understand how I can have the same mapping as my local instance of MAMP apache.
Not being well versed in apache, I do not know how to resolve this issue. I am using the root public_html folder that has been mapped to my user. Is it possible to use a rewrite rule in a .htaccess to do this?
Thanks for your time.
You could use a .htaccess rewrite rule that just directs all images/css/whatever to a specific directory.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /path/relative/to/web-root/
RewriteRule /?([^/]+)\.css css/$1.css
RewriteRule /?([^/]+)\.(jpe?g|png|gif) images/$1.$2
</IfModule>
This is assuming all your images are in a folder called images and all the style-sheets in a folder called css.
In this scenario, it would be best call the images and stylesheets in your code/css using an absolute path. That way images would be cached properly. Even tho the server redirects all the images to the same directory, the client would not see that. So if the same image was called using a relative path from two files in different positions of the tree, the client would see those as two different images and not cache it properly
If your "shared" environment means that you're sharing a DOCUMENT_ROOT, then you'll have to be careful with a .htaccess file - as this will be the .htaccess file for everyone. Otherwise if you have a Virtual host, then what would it take to upload you images into /path/to/document/root/img?
Thanks for your input on this.
Having got through to the web hosting company, I have since realized that the behavior of a virtual host differs if you do not have an ANAME pointing to it. On adding one the folder public_html is mapped as web root as it should be.

Php - name of page parts

i need in naming like this:
site.com/about
site.com/contacts
Could i do it without .htaccess?
If you want a very simple implementation then you could structure your folders to allow you to do something like:
site.com/about/ - which will go to /about/index.php
site.com/contacts/ - which will go to /contacts/index.php
But obviously there's no room for any dynamic URLs, for that you would need a .htaccess implementation. They're very simple to do.
If you mean "Can I configure my webserver to parse /about and /contacts as PHP without using an .htaccess file", then no. Without further configuration (i.e. a local htaccess, or the global configuration files), Apache will not pass them through the PHP handler. You also won't be able to setup these URLs are redirects (internal or otherwise) without configuration (again via htaccess).
You could do it without a .htaccess file if you had access to the apache config files that define your site. However, if you can't use .htaccess files, you likely don't have access to the apache configs either. Might be wroth asking your host / sysadmin.
You might also be able to do it through the 404 handler if your host lets you have access to that.
that is rewriting
google search
similar to:
Options +FollowSymLinks
RewriteEngine on
Rewriterule ^about /menu/about.html
Rewriterule ^contacts /menu/contacts.html

How to make website unavailable for others while developing it online?

Is it possible to make the website unavailable while editing it, while online?
If so, what is the method called and how is it done?
I have a webhosting company so it's not my own server!
Typically, people use the .htaccess to password protect it until you are ready to show it to the world
Here is a link to an article with instructions
http://www.elated.com/articles/password-protecting-your-pages-with-htaccess/
I had the same issue, I just put up a "coming-soon.html" page and redirected everyone without my IP there via htaccess, here's the question on SO: Want to redirect all visitors except for me [.htaccess]
Or, if it's ASP.Net - place a file called app_offline.htm in the site root.
If you are uploading a new version of the site you can create a symlink in the place of the root directory and then switch that when the site's ready. You can also switch the symlink back to the previous version if something goes wrong.
You can make an .htaccess file like this :
RewriteCond $1 !offline.html$
RewriteCond %{REMOTE_ADDR} !^your_ip_adress
RewriteRule ^(.*)$ http://www.mydomain.com/offline.html [R=302,L]
Simply put the files in a new directory and don't tell anybody else the name of the directory. (Or you can tell it to some beta testers if you like.)
When the pages are ready and tested, just move the files into the normal place (after moving the old existing files into backup directory). This way, you always have a working site online.
But generally, it is better to develop the web site on your local computer, not online.

Categories