2 different urls inside a website - php

When I rewrite my url with htaccess, I have 2 differents url.
normal url :
http://localhost/clicshopping_test/boutique/index.php/Products/Description/products_id,1
htaccess :
http://localhost/clicshopping_test/boutique/Products/Description/products_id,1
index.php/ is removed.
The problem is the link inside the website is not rewrited.
I tried to add :
$link = str_replace('index.php/', '',$link);
in this code to have the same link than as url, it does't work.
Do you have an idea for that ?
if ($search_engine_safe === true && defined('SEARCH_ENGINE_FRIENDLY_URLS') && SEARCH_ENGINE_FRIENDLY_URLS == 'true' && SEFU::start() && static::getSite() != 'ClicShoppingAdmin') {
$link = str_replace(['?', '&', '='], ['/', '/', ','], $link);
$link = str_replace('index.php/', '',$link);
}
tk.
the function about the OSCOM::link :
public static function link($page, $parameters = null, $add_session_id = true, $search_engine_safe = true) {
$page = HTML::sanitize($page);
$site = $req_site = static::$site;
if ((strpos($page, '/') !== false) && (preg_match('/^([A-Z][A-Za-z0-9-_]*)\/(.*)$/', $page, $matches) === 1) && CLICSHOPPING::siteExists($matches[1], false)) {
$req_site = $matches[1];
$page = $matches[2];
}
if (!is_bool($add_session_id)) {
$add_session_id = true;
}
if (!is_bool($search_engine_safe)) {
$search_engine_safe = true;
}
if (($add_session_id === true) && ($site !== $req_site)) {
$add_session_id = false;
}
$link = static::getConfig('http_server', $req_site) . static::getConfig('http_path', $req_site) . $page;
if (!empty($parameters)) {
$p = HTML::sanitize($parameters);
$p = str_replace([
"\\", // apps
'{', // product attributes
'}' // product attributes
], [
'%5C',
'%7B',
'%7D'
], $p);
$link .= '?' . $p;
$separator = '&';
} else {
$separator = '?';
}
while((substr($link, -1) == '&') || (substr($link, -1) == '?')) {
$link = substr($link, 0, -1);
}
// Add the session ID when moving from different HTTP and HTTPS servers, or when SID is defined
if (($add_session_id === true) && Registry::exists('Session')) {
$CLICSHOPPING_Session = Registry::get('Session');
if ($CLICSHOPPING_Session->hasStarted() && ($CLICSHOPPING_Session->isForceCookies() === false)) {
if ((strlen(SID) > 0) || (((HTTP::getRequestType() == 'NONSSL') && (parse_url(static::getConfig('http_server', $req_site), PHP_URL_SCHEME) == 'https')) || ((HTTP::getRequestType() == 'SSL') && (parse_url(static::getConfig('http_server', $req_site), PHP_URL_SCHEME) == 'http')))) {
$link .= $separator . HTML::sanitize(session_name() . '=' . session_id());
}
}
}
while(strpos($link, '&&') !== false) {
$link = str_replace('&&', '&', $link);
}
if ($search_engine_safe === true && defined('SEARCH_ENGINE_FRIENDLY_URLS') && SEARCH_ENGINE_FRIENDLY_URLS == 'true' && SEFU::start() && static::getSite() != 'ClicShoppingAdmin') {
$link = str_replace(['?', '&', '='], ['/', '/', ','], $link);
// $link = str_replace('index.php/', '',$link);
}
return $link;
}
function for HTML::link
public static function link($url, $element, $parameters = null) {
return '<a href="' . $url . '"' . (!empty($parameters) ? ' ' . $parameters : '') . '>' . $element . '</a>';
}
the call :
$products_image = HTML::link(OSCOM::link('index.php', 'Products&Description&products_id=' . $products_id), HTML::image($CLICSHOPPING_Template->getDirectoryTemplateImages() . $CLICSHOPPING_ProductsCommon->getProductsImage($products_id), HTML::outputProtected($products_name_image), (int)SMALL_IMAGE_WIDTH, (int)SMALL_IMAGE_HEIGHT));
My htaccess
Options -Indexes
Options +FollowSymlinks
#note for apache2 must be activated becarefull on apache 2.4
#AllowOverride All
#AcceptPathInfo on
#### Your config to change ###############
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
#to change in function your website
RewriteBase /test/boutique/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#to change in function your website
RewriteRule . /test/boutique/index.php [L]
#to change in function your website
#uncomment to have clear url
RewriteCond %{THE_REQUEST} /test/boutique/index\.php/(\S*)\s [NC]
RewriteRule ^ %1 [L,R=301,NE]
</IfModule>

Related

PHP script fails to get correct home page

This script is a function to define site url to send it to the big script but because it is shared hosting it gets incorrect path how can tell it the correct site path ? I wanna tell it the site url and script dir directly the script thinks this is the path /hermes/bosnaweb23a/index.php but the correct path is /index.php only.
I wanna remove /hermes/bosnaweb23a/
because /hermes/bosnaweb23a/ is the shared hosting path
<?php
function home_base_url(){
$base_url = (isset($_SERVER['HTTPS']) &&
$_SERVER['HTTPS']!='off') ? 'https://' : 'http://';
$tmpURL = dirname(__FILE__);
$tmpURL = str_replace(chr(92),'/',$tmpURL);
$tmpURL = str_replace($_SERVER['DOCUMENT_ROOT'],'',$tmpURL);
$tmpURL = ltrim($tmpURL,'/');
$tmpURL = rtrim($tmpURL, '/');
if (strpos($tmpURL,'/')){
$tmpURL = explode('/',$tmpURL);
$tmpURL1 = $tmpURL[0];
$tmpURL2 = $tmpURL[1];
$tmpURL = $tmpURL1;
if(!empty($tmpURL2)) $tmpURL .= '/'.$tmpURL2;
}
if ($tmpURL !== $_SERVER['HTTP_HOST'])
$base_url .= $_SERVER['HTTP_HOST'].'/'.$tmpURL.'/';
else
$base_url .= $tmpURL.'/';
$base_url = str_replace('//','/',$base_url);
$base_url = str_replace('http:/','http://',$base_url);
$base_url = str_replace('https:/','https://',$base_url);
return str_replace(dirname(__FILE__),'',$base_url);
}
$local_path = dirname(__FILE__).'/';
$sSoftware = strtolower( $_SERVER["SERVER_SOFTWARE"] );
function getSlashes() {
$sSoftware = strtolower( $_SERVER["SERVER_SOFTWARE"] );
if ( strpos($sSoftware, "microsoft-iis") !== false ) return "\\";
else return "/";
}
if ( strpos($sSoftware, "microsoft-iis") !== false ) {
$local_path = str_replace(getSlashes(), '/', dirname(__FILE__)).'/';
}
function get_domain() {
return $_SERVER['HTTP_HOST'];
}
$remote_path = home_base_url();
if((strpos($remote_path, '127.0.0.1') !== false) || (strpos($remote_path, 'localhost') !== false)) {
$find = str_replace(' ','',":\ ");
#$local_path = end(explode($find,$local_path));
define('DOCUMENT_ROOT', '/'.$local_path);
}else{
define('DOCUMENT_ROOT', '/'.$local_path);
}
?>
You could get index.php from /hermes/bosnaweb23a/index.php by using this code.
$url = explode("/", "/hermes/bosnaweb23a/index.php");
$script_name = end($url);
$script_name would be index.php.

What is the best approach to scn directories inside condition

Inside a condition, I have some element in static.
$group == 'modules_account_customers'
for example
I would like to make that in dynamic (read).
How to proceed?
Below you an example of this condition.
All the directories start with modules_
I suppose I must use a function like a glob and select all the _modules. I don't know exactly how to start.
Any idea.
Tk.
foreach ( $modules_array as $module ) {
$class = basename($module, '.php');
// module language
if ( !class_exists($class) ) {
if ( MODE_VENTE_PRIVEE == 'true' && $customer->isLoggedOn() ) {
$modules_boxes = 'modules_boxes';
} elseif (MODE_VENTE_PRIVEE == 'true' && !$customer->isLoggedOn ) {
$modules_boxes = '';
} else {
$modules_boxes = 'modules_boxes';
}
if ($group == $modules_boxes ||
$group == 'modules_account_customers' ||
$group == 'modules_account_history_info' ||
$group == 'modules_advanced_search' ||
$group == 'modules_blog' ||
$group == 'modules_blog_content' ||
$group == 'modules_create_account' ||
) { .....}
}
}
I started by this and allow to display the directories inside an array :
public function getReadModulesDefaultDirectories($source_folder = 'modules_') {
$dir = $this->_directoryTemplateSources . '/' . $this->_template . '/' . $this->_codeSail . '/' . $this->_directoryModules;
$module_directories = array_diff(glob($dir . $source_folder . '*', GLOB_ONLYDIR), $exclude);
$result = [];
foreach ($module_directories as $value) {
$result[] = str_replace($dir, '', $value);
}
return $result;
}

protect against xss with .htaccess file

My htaccess redirects everything to my index.php, but i wonder if it's vulnerable to xss attacks. Am i safe?
-- htaccess file
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*$ index.php/$1 [L]
php_value auto_append_file none
-- index.php file ------
<?php
require_once 'dbliteconnect.php';
$req['status'] = (php_sapi_name() == 'cli') ? "true" : 'false';
if (php_sapi_name() == 'cli' || (isset($_SERVER['argc'])) && $_SERVER['argc'] > 0) {
$var = isset($argv[1]) ? $argv[1] : '';
$var = strreplace("/", "", $var);
$req['var'] = $var;
}
if (isset($_SERVER['REQUEST_URI']) && php_sapi_name() != 'cli-server') {
$var = strreplace('/'.basename(DIR_), "", $_SERVER['REQUEST_URI']);
$var = str_replace("/", "", $var);
$var = str_replace(".html", "", $var);
$req['var'] = $var;
}
if (php_sapi_name() == 'cli-server') {
$var = $_SERVER['REQUEST_URI'];
$var = substr($var, 1);
$var = strreplace("/", "", $var);
$var = str_replace(".html", "", $var);
$req['var'] = $var;
}
request($req);
?>

codeigniter - redirect to https and www causes infinite redirect loop

I'm trying to force redirect to www with https from index.php but i keep getting an infinite redirect loop.
Here is my code:
index.php:
$configs = include_once($_SERVER['DOCUMENT_ROOT'] . '/application/config/maintenance.php');
if (stripos($_SERVER['SERVER_PROTOCOL'], 'https') === false && $configs['protocol'] == 'https') {
$protocol = 'https://';
$protocolRedirect = true;
} else {
$protocol = 'http://';
$protocolRedirect = false;
}
if (strtolower(substr($_SERVER['HTTP_HOST'], 0, 4)) !== 'www.') {
$url = $protocol . 'www.' . $_SERVER['HTTP_HOST'];
$wwwRedirect = true;
} else {
$url = $protocol . $_SERVER['HTTP_HOST'];
$wwwRedirect = false;
}
if ($_SERVER['REQUEST_URI'] != '/') {
$url .= $_SERVER['REQUEST_URI'];
}
if ((isset($wwwRedirect) && $wwwRedirect == true) || (isset($protocolRedirect) && $protocolRedirect == true )) {
header('Location: ' . $url);
exit();
}
maintenance.php:
return array(
'protocol' => 'https'
);
Any idea why?
Note: server is running Nginx - but i don't want to redirect thorugh it.

Add trailing slash to url

I'm trying to add a trailing slash to urls using PHP. It can't be done with mod_rewrite since I have something similar to this in .htaccess:
RewriteRule ^page/(.*)$ index.php?page=$1 [L]
and I want to validate that the page exists before 301 redirect with trailing slash.
Right now I'm using this code after validation:
if(substr($_GET['page'], -1) !== '/')
header('Location: http://example.com/'.$_GET['page'].'/'.$_SERVER['QUERY_STRING'],TRUE,301);
But is there any better approach?
Simple way is that just remove the slash if available at the end of url and add it
$str = "http://yoursite.com/testpage";
OR
$str = "http://yoursite.com/testpage/";
echo rtrim($str,"/").'/';
You already have the best solution for this. I would just use $_SERVER['REQUEST_URI'] instead of the already parsed $_GET['page'] and $_SERVER['QUERY_STRING']:
if (substr($_GET['page'], -1) !== '/') {
$parts = explode('?', $_SERVER['REQUEST_URI'], 2);
$uri = 'http://example.com'.$parts[0].'/'.(isset($parts[1]) ? '?'.$parts[1] : '');
header('Location: '.$uri, true, 301);
exit;
}
Here is my universal solution, hope it helps for somebody:
$site_adress = (((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'];
$whole_url = $site_adress . $_SERVER['REQUEST_URI'];
$pos = strpos($whole_url, "?");
$changed_url = FALSE;
if($pos !== FALSE && $whole_url[$pos - 1] != "/") {
$whole_url = substr_replace($whole_url, "/", $pos, 0);
$changed_url = TRUE;
} else if($pos == FALSE && substr($whole_url, -1) != '/') {
$whole_url = $whole_url . "/";
$changed_url = TRUE;
}
if($changed_url) {
header("HTTP/1.1 301 Moved Permanently");
header("Location: " . $whole_url);
exit();
}

Categories