Views: 12397
Last Modified: 17.09.2014
In order to store identifiers of elements/sections of information blocks the field Symbolic Code is most convenient. For example, in the link www.myserver.com/catalog/mobile/nokia_3310/, mobile is the symbol code of the section Mobile telephones, and nokia_3310 is the symbol code of the element located in the section Mobile telephones. The symbol code must be unique, and the system itself checks its uniqueness.
The variable $_SERVER["REQUEST_URI"] in the error 404 handler must be broken down by parameters. To do so, a number of useful functions are available in PHP:
For example, links similar to myserver.com/users/ are processed in the file 404.php as follows:
<?
if(preg_match("~^/users/([a-z_][a-z0-9_]{2,14})/?$~i",$_SERVER["REQUEST_URI"],$match))
{
header("HTTP/1.1 200 OK");
//selection by the identifier
require($_SERVER["DOCUMENT_ROOT"]."/bitrix/header.php");
$res = CUser::GetList($O, $B, Array("LOGIN_EQUAL_EXACT"=>$match[1],"ACTIVE"=>"Y"));
//$match[1] contains login
if($arUser = $res->GetNext())
{
//user’s data are displayed
}
else
{
//error: there is no such user
}
require($_SERVER["DOCUMENT_ROOT"]."/bitrix/footer.php");
}
else
{
header("HTTP/1.1 404 Not Found");
//error
}
?>
But fixed check in preg_match not permit to do links similar to www.myserver.com/users/user_login/?r1=banner&r2=computerra.com that are very much needed to analyze advertising campaigns. That is why we write the following in the beginning of the file 404.php:
<?$arURI = parse_url($_SERVER["REQUEST_URI"]);
$_SERVER["REQUEST_URI"] = $arURI["path"];
if(!empty($arURI["query"]))
{
parse_str($arURI["query"],$par);
foreach($par as $key => $val)
{
global $$key;
$$key = $val;
}
}
?>
Examples
Example 1
The news similar to /about/news/23.html (link for a printable version /about/news/print_23.html) instead of /about/news/detail.php?ID=23 (/about/news/detail.php?ID=23&print=Y)
-
mod_rewrite
RewriteEngine On
RewriteBase /
RewriteRule ^about/news/([0-9]+).html$ about/news/detail.php?ID=$1
RewriteRule ^about/news/print_([0-9]+).html$ about/news/detail.php?ID=$1&print=Y
- Error 404 handler
<?if(preg_match("~^/about/news/(print_)?([0-9]+).html$~",$_SERVER["REQUEST_URI"],$match))
{
header("HTTP/1.1 200 OK");
$_GET["print"] = (strlen($match[1])>0 ? "Y": "");
$_REQUEST["ID"] = $match[2];
include($_SERVER["DOCUMENT_ROOT"]."/about/news/detail.php");
}
else
{
define("ERROR_404", "Y");
header("HTTP/1.1 404 Not Found");
require($_SERVER["DOCUMENT_ROOT"]."/bitrix/header.php");
$APPLICATION->SetTitle("404 - файл не найден");
require($_SERVER["DOCUMENT_ROOT"]."/bitrix/footer.php");
}
?>
Additional Information
How to delete "PHPSESSID=..." from URL?
In order to get rid of the session identifier in URL, uncomment the following line in /.htaccess:
php_flag session.use_trans_sid off
If it brings no result, the parameter value session.use_trans_sid must be changed to Off directly in php.ini on the server.
Also make sure the value of the parameter session.use_cookies is set as On.
How to remove question mark from a page URL?
Follow the following steps:
- Create the file .htaccess with the following contents in the catalog /news/:
ErrorDocument 404 /news/404.php
- Create the file 404.php with the following contents in the catalog /news/:
<?
$arrPath = pathinfo($_SERVER["REQUEST_URI"]);
function initialize_params($url)
{
if (strpos($url,"?")>0)
{
$par = substr($url,strpos($url,"?")+1,strlen($url));
$arr = explode("#",$par);
$par = $arr[0];
$arr1 = explode("&",$par);
foreach ($arr1 as $pair)
{
$arr2 = explode("=",$pair);
global $$arr2[0];
$$arr2[0] = $arr2[1];
}
}
}
initialize_params($_SERVER["REQUEST_URI"]);
require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_before.php");
$arr = explode("?",$arrPath["basename"]);
$fname = $arr[0];
if (strlen(trim($arrPath["extension"]))>0)
{
$arr = explode(".",$fname);
$NEWS_ID = intval($arr[0]);
if ($NEWS_ID>0)
{
$ID = $NEWS_ID;
$APPLICATION->SetTitle("News Details");
$sapi = php_sapi_name();
if ($sapi=="cgi") header("Status: 200 OK"); else header("HTTP/1.1 200 OK");
require_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/iblock/iblock.php");
CIblock::ShowPanel($IBLOCK_ID, $ID);
include($_SERVER["DOCUMENT_ROOT"]."/bitrix/php_interface/include/news/news_detail.php"); // interface script which is accessed also
// in /news/detail.php
}
}
require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/epilog.php");
?>