Change site root directory with php - 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.

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...

.htaccess mapping www & non-www to folder and maintain www

I am no .htaccess expert and extremely rarely use it. I went through several solutions on Net and SO but no luck still. So I have one IP says 1.2.3.4 that has sub folder of my wordpress application says ABC in the /var/www/html/. DNS has been setup so that www.mydomain.com and mydomain.com point to IP 1.2.3.4.
I have my content in .htaccess file as below and it only supports mydomain.com. The content is displayed as expected except when I put www.mydomain.com it displays Linux home page.
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^mydomain\.com$
RewriteCond %{REQUEST_URI} !^/ABC/
RewriteRule (.*) /ABC/$1
When I put www.mydomain.com the browser automatically change the URL to mydomain.com.
Apart from that, I would also like to have all domain with www. Means request from mydomain.com will be displayed as www.mydomain.com. Later I will just put www.mydomain.com in the URL config of my wordpress. Some helps are appreciated.
Switch back to your old .htaccess code. I got it more clear after your last update. You have wordpress files in "public_html/ABC" but need your website url as www.example.com. Let me explain step by step.
Paste your old code in .htaccess.
copy (do not move) the index.php and .htaccess file from the /ABC folder to the "public_html" of your site.
Find one rule in "public_html/index.php" as "require( dirname( FILE ) . '/wp-blog-header.php' );" and replace it with "require( dirname( FILE ) . '/ABC/wp-blog-header.php' );" (do not edit the index.php file in the folder /ABC!!)
change ONLY the "Site Address (URL)" in the general settings (Settings -> General) to "http://www.example.com".
So finally I managed to get what I want to be working with below solution. Hopefully this will help somebody out there:
RewriteEngine on
RewriteBase /
# Whatever request coming to the server, add www to it
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# Map the www URL to the subfolder in root directory
RewriteCond %{HTTP_HOST} www\.myurl\.com$
RewriteCond %{REQUEST_URI} !^/thefolder/
RewriteRule (.*) /thefolder/$1
For Wordpress part, I have put codes in wp-config.php as below:
define('WP_HOME','http://www.myurl.com');
define('WP_SITEURL','http://www.myurl.com');
Thank you. Cheers.
UPDATE
I have just realized that after the changes above, I could not able to login to my WP admin page. This is due to wrong redirect. It looks something like http://www.myurl.com/wp-login.php?redirect_to=http%3A%2F%2Fwww.myurl.com%2Fthefolder%2Fwp-admin%2F&reauth=1
When I remove the thefolder from URL it redirects to expected page. Can somebody advice on this?

htaccess mod_rewrite redirect to directory without displaying on url

I have a site called www.example.com and I have my php files in it. I store all my working files in www.example.com/site. I want to view the site in www.example.com instead, without moving my site content. What can I do?
This is currently what I am typing in .htaccess. It will redirect my site to www.example.com/site but I think the url is ugly
RewriteEngine on
RewriteBase /
RewriteRule ^(/.*|)$ /magento$1 [NC,L]
Let me see if i understood this correctly.
You have a domanin, www.example.com, and on this domain you want to display the content of a directory, www.example.com/site !?
If this is the case then you need to change the document root
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteCond %{REQUEST_URI} !^/site
RewriteRule ^(.*)$ /site/$1 [L]
or
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)$ /site/$1 [L,NC]
There are 2 things you must consider :
If I understand your request, you are accessing some PHP files using www.example.com, but what you want is to access www.example.com/site, but without the /site, right ?
So basicaly, what you're looking for is NOT rewrite, it's just pointing your domain to the good folder, which is /site, right ?
If you're using Apache2, you have to edit your apache's configuration file in /etc/apache2/site-available/default (or remplace default with the name of the virtualhost you may have created).
In this file, look for the directive DocumentRoot. It should lead to the "root" directory of your pages (the one you access typing www.example.com)
I think you just have to append /site to this DocumentRoot and then reload your apache2 with service apache2 reload
You're website www.example.com will now lead to the correct directory.
If it's still not working, you must consider looking into magento's admin, because magento is rewriting url according to the Base URL you specify inside Admin / Configuration / general / web.
You'll have to modify Base_URL in both Secure and Un-Secure sections.
Then it should work fine.
I would comment out the Rewrite bloc you're using at the moment, or maybe I didn't fully undestand what you want to achieve.

.htaccess for site in sub-directory using Yii framework

I have looked at several examples of htaccess configs for websites within sub-directories, and tried most of them without 100% success.
My setup is:
using Yii framework
htaccess at public_html/.htaccess
site located inside public_html/mysite directory
index handling all requests located at public_html/mysite/frontend/www/index.php
The status of the URLs:
www.mysite.com works fine [ok]
www.mysite.com/controller/action shows me the homepage [wrong]
www.mysite.com/mysite/frontend/www/controller/action works fine [wrong, the item above should work instead]
My .htaccess at the moment looks like this:
AddHandler application/x-httpd-php53s .php .html
Options +SymLinksIfOwnerMatch
IndexIgnore */*
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www.)?mysite.com$
RewriteCond %{REQUEST_URI} !^/mysite/frontend/www
RewriteRule ^(.*)?$ /mysite/frontend/www/index.php [L]
I have tried everything, but I have no idea why www.mysite.com/controller/action won't work :(
Any help would be really appreciated! Thanks!
I found the answer to this similar question to be helpful. Here is how my rewrite rules ended up:
#Forward all non-existent files/directories to Yii
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) subdir/index.php/$1 [QSA,L]
This takes all non-existent files/folders and sends them to the yii script with initial url appended. QSA appends any query string that may be present in the initial url.
You didn't mention if you configured Yii's Url Manager for clean URLs. You need to, otherwise Yii expects the "route" to appear as a GET param named "r". If you didn't, consult this section of the definitive guide
You dont need to edit .htaccess. You just need to move the Yii entry script (index.php) and the default .htaccess up from the subdirectory to the webroot (so that they reside directly under public_html). Once you move index.php and .htaccess to the root directory, all web requests will be routed directly to index.php (rather than to the subdirectory), thus eliminating the /subdirectory part of the url.
After you move the files, you will need to edit index.php to update the references to the yii.php file (under the Yii framework directory) as well as the Yii config file (main.php). Lastly, you will need to move the assets directory to directly the webroot, since by default, Yii expects the assets directory to be located in the same location as the entry script).
That should be all you need to do, but if you need more details, I describe the approach fully here:
http://muhammadatt.tumblr.com/post/83149364519/modifying-a-yii-application-to-run-from-a-subdirectory
I also didn't update the .htaccess file, easier to modify the httpd.conf virtual host for the subdomain and change the DocumentRoot to point to your yii folder.

htaccess mod_rewrite help

i have a domain with a website in a folder.
i.e
http://domain.com.au/website/page.php
i only have access to .htaccess files to make the rules.
i would like my url to look like this..
http://domain.com.au/page
so in essence i want to drop the subfolder its sitting in, and the php extention.
All links in my php are structured like this though
page link
this is the same for js and css. they are all referenced from the 'website' folder.
will the rewrite mean i have to change all my links?
priority is dropping the folder from the url, not so much the php extension.
1) You could move all the files to the root web directory and run the website from there.
2) You would have to manually modify every link to remove the website directory from the beginning and then use the .htaccess file to redirect the pages back to the appropriate page. If you're going to do it this way, you might as well remove the PHP extension...
RewriteEngine On
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^domain.com.au$ [NC]
RewriteRule ^(.*)/ website/$1.php [L]
You may have to modify the website/$1.php part to include your entire root directory in some cases, but that should work as long as the .htaccess file is in the root web directory. That will redirect any page in the form 'http://domain.com.au/page' to 'http://domain.com.au/website/page.php' while keeping the URL the same in the address bar.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^page\_one/(.*)/([0-9]{1,6})/?$ webroot/products/products/view/$2 [L]
</IfModule>
this htaccess code redirects all trafic from www.domain.com/page_one/anything/numeral
to the page www.domain.com/webroot/products/products/view/same numeral
for your example, something like
RewriteRule ^page?$ website/page.php$2 [L]
note, this works for "page" as a string, if you need other functions , change it
for more insight, you might want to take a look at this link :
http://www.javascriptkit.com/howto/htaccess.shtml

Categories