Tuesday, September 27, 2016

PHP Document Root,Path and URL detection

Hello Friends,
                       I am back again bring for you a common  things  which you have face in your day to day work environment . Today I have brings you to how we can find the various path in Php . Many a times you came across with the path problem in php when you are developing your website or a php Project. I have brought you a list of function which you can  make use of these to make your website url dynamically you don't need to make it fixed as when you are working on live website . some people fixed there url which is not a good programming techniques. Below are the list of some function in php.


$base_dir  = __DIR__; // Absolute path to your installation , ex: /var/www/abcwebsite

$doc_root  = preg_replace("!${_SERVER['SCRIPT_NAME']}$!", '',

$_SERVER['SCRIPT_FILENAME']); # ex: /var/www

$base_url  = preg_replace("!^${doc_root}!", '', $base_dir); # ex: '' or '/abcwebsite'

$protocol  = empty($_SERVER['HTTPS']) ? 'http' : 'https';

$port      = $_SERVER['SERVER_PORT'];

$disp_port = ($protocol == 'http' && $port == 80 || $protocol == 'https' && $port == 443) ? '' : ":$port";

$domain    = $_SERVER['SERVER_NAME'];

$full_url  = "${protocol}://${domain}${disp_port}${base_url}"; # Ex: 'http://mysite.com',
'https://mysite.com/abcwebsite', etc.

Below are the list of Some File redirect function which you basically used in  your htaccess file.

#301 Redirects for .htaccess

#Redirect a single page:
Redirect 301 /pagename.php http://www.abc.com/mypagename.html

#Redirect an entire site:
Redirect 301 / http://www.abc.com/

#Redirect an entire site to a sub folder
Redirect 301 / http://www.abc.com/subfolder/

#Redirect a sub folder to another site
Redirect 301 /subfolder http://www.abc.com/

#This will redirect any file with the .html extension to use the same filename but use the .php extension instead.
RedirectMatch 301 (.*)\.html$ http://www.abc.com$1.php


Example
Variable Content
base_dir /var/www/abcwebsite
doc_root /var/www
base_url /abcwebsite
protocol http
port 8080
domain myexample.com
full_url http://myexample.com:8080/abcwebsite

No comments:

Post a Comment