Clean up index files and place in directory? - php

Right now I have a assortment of 9 files (javascript, image, stylesheet, php) in the root directory of my web server. I would like too put all of these files in a directory called home (for home page). So now at this point I can view my home page at http://example.com/home.
I would like it so that when you visit http://example.com/ it points to the files in the home directory.
I am using php so my first attempt was to create a index.php in the root and include the index from home. This breaks relative URLS within styles, javascript, and includes. To combat this I prepended my includes with $_SERVER['DOCUMENT_ROOT'].'/home/. More problems arise when I try to redirect http://example.com/home to http://example.com/.
Is their any better way of doing this, possibly with .htaccess?

Yes, if your server supports mod_rewrite, you could probably add a RewriteRule:
RewriteRule /(.*) /home/$1 [R,L]

In your Apache httpd.conf change the DocumentRoot to your "home" folder.

Related

Folder structure website php

Lets say I have the website url www.example.com. My index.php page is in the root directory. I then want to have a page at www.example.com/someDir as well as a page at www.example.com/someDir/anotherDir. I thought that I would make the directory someDir in the root directory and make the directory anotherDir within the directory someDir, then place an index.php page in both those new directories which would be the visible page at those URLs.
Is this the proper way?
Yes.
Note that your web server (apache or whatever) should redirect www.example.com/someDir to www.example.com/someDir/ (with a trailing slash) which will make the relative links work correctly with respect to the index.php files.

Including web page's index.php in another directory.

Assume we have a complete web page in /web_page/ directory. Now I want to include the index.php file from /include/ directory, so that /include/index.php will show the same web page as in /web_page/index.php. There are some problems:
This can not be done through iframe.
There are a lot of included files in the /web_page/ directory and simple require_once() causes other includes to be not found.
As #pes502 suggested, it may be better to do this with .htaccess. Try adding this to a .htacess file in the root directory.
RewriteEngine On
RedirectMatch 301 /include/index.php /web_page/index.php
This will send a header that redirects from one page to the other, preventing duplicate pages.
You can use ../ as 'one folder up'. So include should be like:
include_once('../include/index.php');

In my wordpress site I want to start somefile.html first and after 10 seconds redirect to index.php file

I have a wordpress site http://hamroletang.com and it contain a file http://hamroletang.com/sardanjali.html
Now I want sardanjali.html to be my website landing page.
Then it will redirect to index.php which i have done already
The easiest way would be to rename sardanjali.html to index.html and add:
<meta http-equiv="refresh" content="5;URL='http://hamroletang.com/index.php'">
Then, make sure that in your Apache config .html takes precedence over .php
You can rename sardanjali.html to index.html and then change in the apache config the directory index to have index.html before index.php:
DirectoryIndex index.html index.php
On my system this was already the default (ubuntu).
In your redirect you should explicitly redirect to index.php, and not to the domain in general.
If you cannot change apache config files and renaming to index.html doesn't help, then you can move your blog to a subdomain or subdirectory and redirect into that. But that would break all the links to your blog. Probably not what you want.
You can change the default page of your website to whatever you want it to be. In your case specify the single file sardanjali.html as your default page. The settings vary between web servers. e.g. on Apache here are the instructions - http://www.cyberciti.biz/faq/apache-display-or-change-a-default-page-other-than-indexhtml/
And yes, you have to use the refresh in your sardanjali.html to automatically redirect the user to your index.php after the specified time.

unwanted URL switch and security concern. looking for a simpler or better solution

I couldn't do it any shorter :-/
I'm having a problem with my php project concerning redirects and $_GET.
I hope you will help me :)
Lets say I have these 3 folders in the root of my Webserver.
/root
/themes
/page.php
/project1
/index.php
/content.php
/project2
/index.php
/content.php
/themes contains a php file called /page.php
/page.php contains a dynamic PHP/HTML structure or theme, which I can use for different content files.
/project1 and /project2 contain mostly HTML content files which get included by /page.php.
/project1 and /project2 are the root folders for 2 different domains, so each contains an index.php.
Now the problem:
In order to get the websites for each domain to work I have to redirect the user to /themes/page.php, because thats where the functionality is and then include the correct content files.
To let /page.php know what content file out of /project1 or /project2 it has to load I use a redirect for index.php to fill $_GET ,like this :
header("Location: root-domain/themes/page.php?path=project1&file=content1");
That way page.php gets to know the path and filename of the requested file, by taking it from GET.
BUT:
That way the user gets to know each folder and each filename, even of some php files. I don't know if that's good?
And the URL changes from the requested URL into the URL of my webserver root.
Does one know a better solution?
Thanks! <3
Actually, I think there is a solution, you just need to use mod_rewrite, which is a module for Apache. With that, you could redirect all your URL requests to this one specific page of yours. Unfortunatelly, the "magic" behind mod_rewrite is little bit more complex, so I recommend you this documentation for further uses.
But for now, just check if you have a mod_rewrite turned on in server configuration, then simply create a file called .htaccess in directory project1 (or any other) and insert this little piece of code in it:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^([^/.]+)/?([^/.]+)?/?$ /themes/page.php?path=$1&file=$2 [QSA]
</IfModule>
Hit save, and that should be it. Now every page request goes through /themes/page.php.
And by the way, you definitely shouldn't be exposing script names in url.

Change base HTTP requested directory -- CPanel

I have a php websites hosted on a server. I use CPanel to manage it. public_html has lets say following directory structure
public_html
- dir1
- dir2
- dir3
- ....other files.....
- website2home
Now I am trying to make website2home as the base directory of my website, but files inside website2home use some files from public_html folder and some files from within itself.
THE PROBLEM
When I assign a domain name to website2home, It does not reads the files from public_html folder (in fact, public_html folder is not visible) and shows some php warnings and some 404 not found errors. But lets say the domain name for original website is www.aaa.com, then if I access website2home by using www.aaa.com/website2home all works fine.
So My Question...
Is there any way to set the base directory, so that my website2home fetches all files and correct files without replacing hundreds of filepaths in php code?
Please answer considering that I don't want to modify source files of website.
I believe you are looking for some kind of .htaccess configuration. For instance, you may set your website2home folder as base folder for your www.aaa.com domain and then redirect some file access to other folders using .htaccess, something like this..
RewriteEngine on
RewriteRule captcha.jpg ../captcha.jpg
RewriteRule \.pdf$ ../pdf_files/$1
Have a look to mod_rewrite documentation
Edit: If, for some reason, the ../ redirection doesn't work, you always can redirect all your files to a pseudo-index PHP page. For instance:
RewriteEngine on
RewriteRule captcha.jpg my_index.php?access=../captcha.jpg
RewriteRule \.pdf$ my_index.php?access=../pdf_files/$1
And then, in your index PHP page you can simply load your PHP files in other folder:
require "../pdf_files/$requested";

Categories