CodeIgniter: CSS styles broken on IP address vs domain name - php

I have this working base_url setting in my
Config.php:
$protocol = is_https() ? "https://" : "http://";
$host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : "";
if(is_cli())
{
$config['base_url'] = '';
}
else if( stristr($host, "localhost") !== FALSE || (stristr($host, '192.168.') !== FALSE) || (stristr($host, '127.0.0') !== FALSE) )
{ // if local
$config['base_url'] = $protocol.$host."/project2/";
}
else
{ // if server
$allowed_hosts = ['website.com', 'www.website.com'];
$config['base_url'] = in_array($host, $allowed_hosts) ? $protocol.$host."/project2/" : "unknown-host.com";
}
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI'; // changing to AUTO didn't work too
// possible base_url results are http://www.website.com/project2 and http://192.xxx.xxx.xxx/project2
My project2/.htaccess code:
RewriteEngine on
RewriteCond $1 !^(index\.php|public|\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1
When I run the site at www.website.com/project2 - everything is fine, but when I run at 192.xxx.xxx.xxx/project2 - css styles are broken
My css links are like this:
<link rel="stylesheet" href="<?php echo base_url();?>assets/stylesheets/somestyle.css" />
How can I fix this?

Silly me! I just changed permissions (to grant all) on my .css files and all worked well now!
My CSS links we're blocked by a file permission that's why my links are broken.
Thanks you guys for all the hints specially on the view-source part. Really helped me a lot!

Related

Clean and friendly URL

Similar question asked here: Htaccess rule to make urls like this ?page=1 look like /page/1 in Codeigniter
But I can't get the answer right.
My routes.php
$route[$key] = "home_controller/index";
$route['page/(:num)'] = 'home_controller/index/$1';
My .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
My config.php
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
$config['base_url'] = $root;
$config['index_page'] = '';
$config['uri_protocol'] = 'PATH_INFO';
Can't understand the syntax.
Been looking all over in Codeigniter Docs such as; https://codeigniter.com/userguide3/general/routing.html
I just can't seem to find the solution for this.
host/page/1
Is working but not redirecting to the desired page in other words its not corresponding to it's page in the pagination
host/?page=1
Is working but I simply want to get rid of ?page=
I also need to fix a path url for host/category/?page=1
to host/category/page/1
I check your code,
As I notice your routing was fine. But your mistake in the config.php file.
Please, replace your code from
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']), '',
$_SERVER['SCRIPT_NAME']);
$config['base_url'] = $root;
$config['index_page'] = '';
$config['uri_protocol'] = 'PATH_INFO';
to
if (!empty($_SERVER['HTTPS']) && ('on' == $_SERVER['HTTPS'])) {
$uri = 'https://';
} else {
$uri = 'http://';
}
$uri .= $_SERVER['HTTP_HOST'];
$config['base_url'] = $uri;
$config['base_url'] .=
preg_replace('#/+$#','',dirname($_SERVER['SCRIPT_NAME'])).'/';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';

limit subdirectories to logged in users

How can i do the below probably .htaccess ?
/sp/sitea => /check.php?page=sitea #no trailing forward slash
/sp/sitea/ => /check.php?page=sitea #trailing forward slash
/sp/sitea/index.php => /check.php?page=sitea/index.php #includes a file
/sp/siteb => /check.php?page=siteb
/sp/index.php , /sp/login.php => no redirect
I then want to check the DB using PHP for sitea to see if the user is logged in and then redirect to /sp/sitea If they are not logged in for that site redirect to /login.php
I tried .htaccess below , in sp folder, but doesnt redirect, im not really .htaccess master
RewriteEngine On
RewriteCond %{REQUEST_URI} /
RewriteRule ^\/(.*)$ sp/check.php?path=$1 [L]
I solved this with a modified .htaccess file
RewriteEngine on
RewriteOptions MaxRedirects=2
RewriteBase /
#dont check non pages
RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png|css|js)$
RewriteRule ^(.*)/(.*)$ /sp/check.php?path=$1&path2=$2 [L,P] #P keeps address in bar after redirect
check.php
<?php
//htaccess redirects here for sitepreview subdomains, then do authentication check and output the html
//cant redirect to page or get redirect loop
session_start();
$dir = $_GET["path"];
$dir2 = $_GET["path2"];
//echo $dir;
if(!isset($_SESSION['login_user'])){
header("Location: /sp/index.php",true,301);
exit();
}else {
$name = $_SESSION['login_user']["name"];
if($name == $dir) {
$homepage = file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/sp/$dir/" . (isset($dir2) && strpos($dir2, ".htm") ? $dir2 : "index.html") );
echo $homepage;
}
else {
header("Location: /sp/index.php",true,301);
exit();
}
exit();
}

how to create good htaccess

Excuse me , I just start to learn about .htaccess files . I'm trying to write it this way (my htaccess file)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /exmpl/vadik_route/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
ReqriteRule ^(.*)$ index.php/$l
</IfModule>
and my index.php
<?php
echo $_SERVER['PATH_INFO'];
?>
but server through this error.
Server error!
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there was an error in a CGI script.
If you think this is a server error, please contact the webmaster.
Error 500
localhost
Apache/2.4.17 (Win32) OpenSSL/1.0.2d PHP/5.6.15
How can I fix that
Change your .htaccess with this one:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
you can send the url by get method for example getme.php?url=/controller/model/
and you can take the url also divide by slash
$_GET['url'] //it is going to show you the url.
lets look at the .htaccess and explain it.
RewriteEngine On
RewriteBase / #dont forget to modify this part. it is explain which folder you project in
RewriteEngine On Options All -Indexes RewriteBase /directoryname/ RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
############### SEO ##########################
#http://www.example.com/hello/booboo/ it takes the url after .com/
RewriteRule ^(.*)$ getme.php?url=$1 [QSA,L]
now we sent all the url to getme.php and we can use it
getme.php
<?php
//we redirect to get in url=$1 so our get method name is url
$parca = explode("/", $_GET["url"]); //and we divided the url by slash .
echo $parca[0];//this is first part "/hello/
echo $parca[1];// and this is second part "/booboo/;
?>
<?php
session_start();
//Define Language file paths
define("LANG_EN_PATH", $_SERVER['DOCUMENT_ROOT'] . '/exmpl/sayt/multilang/en/');
define("LANG_RU_PATH", $_SERVER['DOCUMENT_ROOT'] . '/exmpl/sayt/multilang/ru/');
define("LANG_KR_PATH", $_SERVER['DOCUMENT_ROOT'] . '/exmpl/sayt/multilang/kr/');
define("LANG_TR_PATH", $_SERVER['DOCUMENT_ROOT'] . '/exmpl/sayt/multilang/tr/');
if (isset($_GET['lang']))
{
// GET request found
if ($_GET['lang'] == 'ru')
{
include LANG_RU_PATH . 'ru.php';
$_SESSION['lang'] = 'ru';
}
else if ($_GET['lang']=='en') {
include LANG_EN_PATH .'en.php';
$_SESSION['lang'] = 'en';
}
else if ($_GET['lang']=='tr') {
include LANG_TR_PATH .'tr.php';
$_SESSION['lang'] = 'tr';
}
else
{
include LANG_KR_PATH . 'kr.php';
$_SESSION['lang'] = 'kr';
}
}
//translate for russian
else if (isset($_SESSION['lang']))
{
if ($_SESSION['lang'] == 'ru')
{
include LANG_RU_PATH . 'ru.php';
}
//translate for kyrg
else if($_SESSION['lang'] == 'en')
{
include LANG_KR_PATH . 'en.php';
$_SESSION['lang'] = 'en';
}
// translate for turkish
if($_SESSION['lang'] == 'tr')
{
include LANG_TR_PATH . 'tr.php';
$_SESSION['lang'] = 'tr';
}
else {
include LANG_KR_PATH . 'kr.php';
}
}
else
{
include LANG_KR_PATH . 'kr.php';
$_SESSION['lang'] = 'kr';
}
?>
could you write path for this , as clear url

Url rewrite, htaccess and PHP how to do it

I have a problem with this url and I want to know how to rewrite that into htaccess. I'm pretty new into this stuff so I'm curious on how to do this, for example this url:
WEBSITE/?page=online
and rewrite it to, for example:
WEBSITE/page/online
or this url:
WEBSITE/?page=account&hide=x
to, for example:
WEBSITE/page/account/hide/x
I use this PHP script to include the pages inside one HTML document:
<?php
if (empty($_GET))
{
include 'pages/index.php';
} else {
if (!file_exists('pages/' . $_GET['page'] . '.php' ))
{
header("Location: /");
} else {
include 'pages/' . $_GET['page'] . '.php';
}
}
?>
In your .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Match SITE/page/account/hide/x, etc
RewriteRule ^/?page/([^\.*]+)/([^\.*]+)/([^\.*]+)?$ index.php?page=$1&$2=$3 [L,QSA]
# Match /SITE/page/online etc.
RewriteRule ^/?page/([^\.*]+)/?$ index.php?page=$1 [L,QSA]
You could do something like this.
.htaccess file
RewriteEngine On
RewriteRule (.*) control.php [L]
This says take every request coming to your server and sends it into the control.php file. Then in control.php we process all the gets to directories. You'll probably want some fallbacks, e.g. no GET request; should it go to the root, 404, 403?
control.php
<?php
$parts = '';
foreach($_GET as $key => $directories) {
$parts .= $key . '/' . $directories . '/';
}
header('Location: ' . '/' . $parts);
?>

htaccess rewrite or php url exploding

I am trying/attempting to create my own MVC. I am really trying to manipulate the url. I am trying to get several parameters to form a full URL.
I want to turn
http://example.com/?action=account&user=JohnDoe
to
http://example.com/account/JohnDoe
But I cant seem to get the .htaccess file to work right :/
This is what I have
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-=_?]+)/?$ index.php?action=$1&user=$2 [NC,L]
When I go to http://example.com/account/JohnDoe I get a 404 error.
Your missing one parameter, try this
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?action=$1&user=$2 [NC,L]
Rather than set up a mod_rewrite rule for that specific URL, why not put an entire framework in place?
mod_rewrite as follows:
#URI PATH CONSTRUCTION
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*)$ /index.php?string=$1 [L,QSA]
</IfModule>
Then have your index.php file break up $_REQUEST['string'] into an array that you can test to direct the request to where it needs to go
if (isset($_GET['string'])) {
//DEALS WITH GET PARAMETERS
if (strstr($_GET['string'],"?") !== false) {
$pos = 0;
$tailstr = substr($_GET['string'],$pos);
$endpos = strpos($tailstr, "?");
$endpos = $endpos + strlen("?");
$string = substr($_GET['string'],$pos,$endpos);
} else {
$string = $_GET['string'];
}
$path = explode("/",$_GET['string']);
}
//Depth to 5 levels
for ($i=0;$i<5;$i++) {
if (!isset($path[$i])) $path[$i] = null;
}
global $path;
You now have a global array containing the individual elements in the URI
eg
http://example.com/account/JohnDoe
would give you:
$path[0] = 'account'
$path[1] = 'JohnDoe'
$path[2] = ''
$path[3] = ''
$path[4] = ''

Categories