Views: 6118
Last Modified: 19.05.2022

Example

News of the type as follows: /about/news/23.html (link for print /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
    
  • 404 error 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 - file not found");
    require($_SERVER["DOCUMENT_ROOT"]."/bitrix/footer.php");
    }
    ?>

Additional

How to remove "PHPSESSID=..." from URL?

To discard identifier from session in URL, comment the string in /.htaccess:

php_flag session.use_trans_sid off

If this gives no result, you need to update parameter value session.use_trans_sid to Off directly in php.ini on server.

Make sure that parameter value session.use_cookies is set to On.


How to remove from page URL a question mark character?

The following steps are required to achieve that:

  • create in catalog /news/ the file .htaccess with the following content:
    ErrorDocument 404 /news/404.php
  • create in catalog /news/ the file 404.php with the following content:
    <?
    $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 that is called 
                                                                                            //in /news/detail.php
    }
    
    }
    require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/epilog.php");
    ?>



Courses developed by Bitrix24