URL rewrite
URL rewrite concept
UrlRewrite is used to define script as responding not only to its direct address, but to any other specified URL address. For example, to define URL address settings, you can specify script in the file /fld/c.php, as responding to the address
/fld/c.php?id=15
also responded to the address
/catalog/15.php
Address for script to respond shall not exist on the server. If such address physically exists, the script will be called by this address. URL rewriting system won't be initiated in this case.
Rewriting rules
The URL rewriting rules are configured for each site individually and stored in urlrewrite.php in the site root. This file contains an array $arUrlRewrite, each entry being a URL rewriting rule. The following code shows an urlrewrite.php example:
<? $arUrlRewrite = array( array( "CONDITION" => "#^/gallery/#", "RULE" => "", "ID" => "bitrix:photo", "PATH" => "/max/images/index.php", ), array( "CONDITION" => "#^/forum/#", "ID" => "bitrix:forum", "PATH" => "/forum/index.php", ), array( "CONDITION" => "#^/index/([0-9]+)/([0-9]+)/#", "RULE" => "mode=read&CID=$1&GID=$2", "ID" => "bitrix:catalog.section", "PATH" => "/newforum/index.php", ), array( "CONDITION" => "#(.+?)\\.html(.*)#", "RULE" => "$1.php$2", ), ); ?>
Each rule must contain a unique condition for the rule (within a site). This conditions is written into the key "CONDITION" of array and is a template for Perl-compatible regular expression. For example, the condition:
"CONDITION" => "#^/index/([0-9]+)/([0-9]+)/#"
indicates, that this rule must be applied for all URLs that start with the substrings as follows:
/index/<number>/<number>/
The rule must contain a direct URL for existing script, connected when condition is satisfied. This address is written into the key "PATH". For example, if the URL processing system has registered the rule as follows:
array( "CONDITION" => "#^/gallery/#", "PATH" => "/max/images/index.php", )
and user has requested the page:
/gallery/38.php
which physically doesn't exist, the URL processing system will connect the script as follows:
/max/images/index.php
The rule can include the rewriting rule, written into the key "RULE". If rewriting rule is defined, the URL for the physically existing connected script is generated by replacing the execution condition (expression template) with physical path concatenation via regular condition expression (the key "PATH") and rewrite rule (key "RULE"). For example, if the address rewrite system registers the rule:
array( "CONDITION" => "#^/index/([0-9]+)/([0-9]+)/#", "RULE" => "mode=read&CID=$1&GID=$2", "PATH" => "/newforum/index.php", )
and the user requests the page:
/index/5/48/
the system executes the code for generating script address to be connected:
$url = preg_replace("#^/index/([0-9]+)/([0-9]+)/#", "/newforum/index.php?mode=read&CID=$1&GID=$2", "/index/5/48/");
and connects the script:
/newforum/index.php?mode=read&CID=5&GID=48
If the address rewrite system registers the rule:
array( "CONDITION" => "#(.+?)\\.html(.*)#", "RULE" => "$1.php$2", )
and user requests the page:
/about/company.html?show
executes the code for generating script address to be connected:
$url = preg_replace("#(.+?)\\.html(.*)#", "$1.php$2", "/about/company.html?show");
and connects the script as follows:
/about/company.php?show
The rule can contain the component name which had created this rule. This name is written to the key "ID". In case of automatic re-creating of the rules file urlrewrite.php, the tools of site admin section re-create only the rules which have the completed key "ID". These rules are re-created based on analysis of physical files located in the site folder. Rules with empty "ID" key aren't applied when re-creating the urlrewrite.php file automatically.
Activating URL rewrite engine
You need to activate the URL rewrite engine at the site before use. The following actions must be completed:
- if your web server is configured to trap the 404 error (e.g. directive Apache
ErrorDocument 404 /404.php
for Apache), you will have to edit the file /404.php, by inserting the following command at the very beginning of the file:include_once($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/main/include/urlrewrite.php');
- if you use mod_rewrite with Apache, specify the following in its settings (e.g. in .htaccess):
<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-l RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !/bitrix/urlrewrite.php$ RewriteRule ^(.*)$ /bitrix/urlrewrite.php [L] </IfModule>
Example for rules and conditions for the module mod_rewrite
- Forwarding settings (301 redirect) to folders with "slash" ( / ) character at the end:
RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} !\..{1,10}$ RewriteCond %{REQUEST_URI} !(.*)/$ RewriteRule ^(.*)$ http://[your_site_name]/$1/ [L,R=301]
- Forwarding (301 redirect) from index page (index.php) to the (root) folder for all site pages:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/ RewriteRule ^(.*)index\.php$ http://[имя_вашего_сайта]/$1 [R=301,L]
Components 2.0 Support/h3>
When a SEF-enabled component is added to a page and latter is saved using API functions, a corresponding URL rewrite rule is created automatically. However, if a page is created not using API functions (e.g. uploaded via FTP), you will have to re-create rules manually by using tools in the URL rewrite administration form.
In a component, SEF support can be enabled by specifying a special input parameter SEF_MODE. This will set the predefined parameter SEF_FOLDER to the folder in which the component runs. The folder can be virtual (i.e. it may or may not physically exist). When a page with a SEF-enabled component (SEF_MODE is Y), is saved, a URL rewrite rule is created in the following way: the CONDITION key is set to a regular expression obtained from the SEF_FOLDER, the key "ID" is set to the component name and the path key ("PATH") is set to the physical address of the page.
For example, the "bitrix:catalog" component is added to /fld/c.php. It is activated by the following call:
$APPLICATION->IncludeComponent( "bitrix:catalog", "", Array( "SEF_MODE" => "Y", "SEF_FOLDER" => "/mycatalog/", "IBLOCK_TYPE_ID" => "catalog", "BASKET_PAGE_TEMPLATE" => "/personal/basket.php", ) );
When a page /fld/c.php is saved, the following record will appear in the URL rewrite rules:
array( "CONDITION" => "#^/mycatalog/#", "RULE" => "", "ID" => "bitrix:catalog", "PATH" => "/fld/c.php", )
Thus, requesting a page starting from /mycatalog/, will return the file /fld/c.php. This script may analyse the requested address and act appropriately.
See Also
- URL SEF configuration in Bitrix Framework learning course.
- Complex component and SEF mode in Bitrix Framework learning course.