📜 ⬆️ ⬇️

Practice using mod_rewrite

The article is intended for those who are already familiar with the Apache Rewrite module and, if not always, but use it in their difficult lives. The question is considered in the context of using PHP as a server-side scripting language.

Not finding a suitable article on Habré decided to fill this gap and elaborate on such a wonderful tool as mod_rewrite for Apache. I will not describe all the intricacies of building beautiful URLs and describe the process of the POSIX-like parser of regular expressions in Apache. In this, I hope, not the last article on mod_rewrite, I would like to elaborate on the problem of use. In short, why mod_rewrite and what it gives with a few examples.

Problemma


In my work, I very often came across funny moments when many programmers, either from ignorance, or from the reluctance to use the tools given to them in full force, began to fence around in their sources. As a concrete example, in practically any “multi-platform multitasking script, be it a CMS or something else of this kind, I, in any case, found certain methods for parsing the incoming URL into components.
')
But in any, even the largest project, the number of URLs for the transition is limited to any set. And if we consider that most of them contain duplicate elements (for example, id or some other “substitute”) that can be perfectly passed through a regular expression, we get a very small set of “fundamental” URLs, which built the whole project.

Actually, the absence of mod_rewrite on the hosting of your project has long been considered a move. So why not start using this great tool?

Decision


This is where the developer clearly has a problem - either to learn mod_rewrite, or to use / write a URL parser on the side of the scripting language.

The main and primary mistake is that units are used for this unfamiliar mod_rewrite.

1.Parser URL

:

RewriteRule ^(.*)$ index.php?param=$1 [QSA,L]

:

RewriteRule ^/pages/([0-9A-z_]{1,64}).html$ index.php?mod=Pages&page_link=$1 [QSA,L]

:
  1. , . mod_rewrite , , .
  2. , , , , , . URL — mod_rewrite. , .

2.

mod_rewrite — . , . — . , , , URL, id id ?

, . mod_rewrite :

RewriteRule ^/products/([0-9]{1,8}).html$ index.php?mod=Products&product_id=$1 [QSA,L]
RewriteRule ^(.*)$ index.php?mod=Errors&error_id=404 [QSA,L]

, , . ? , , , , Apache. — , - .

3. -

, mod_rewrite .

RewriteRule ^(.*)/(.*)/var=([^/]+)/ $1/$2 [E=VAR:$3]
Apache , GET-. , , POST GET , -.

„“ — .


mod_rewrite :
  1. Apache Module mod_rewrite
  2. URL Rewriting Guide
  3. URL Rewriting Guide — Advanced topics

mod_rewrite phpclub.ru.


, Apache mod_rewrite — Web- Linux-Apache based . . , .

P.S.
1. , . Rewrite Guide. — .
2. - — , mod_rewrite , - . — .
3. . — ^_^

UPDATE


. , . .

Test Cases:

1. PHP Native
.htaccess:
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
RewriteRule ^(.*)$ index.php?params=$1 [QSA]

index.php:
<?php
$URI = $_GET['params'];
$params = array_values(array_filter(explode('/', $URI)));
print_r($params);


2. Apache mod_rewrite
.htaccess:
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
RewriteRule ^([A-z0-9]{0,})/([A-z0-9]{0,})/([A-z0-9]{0,})/([A-z0-9]{0,})/([A-z0-9]{0,})/([A-z0-9]{0,})/([A-z0-9]{0,})/([A-z0-9]{0,})/$ index.php?0=$1&1=$2&2=$3&3=$4&4=$5&5=$6&6=$7&7=$8 [QSA,L]

index.php:
<?php
print_r($_GET);


3. PHP preg_match
htaccess:
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
RewriteRule ^(.*)$ index.php?params=$1 [QSA]


index.php:
<?php
$URI = $_GET['params'];
preg_match ( "/([A-z0-9]{0,})\/([A-z0-9]{0,})\/([A-z0-9]{0,})\/([A-z0-9]{0,})\/([A-z0-9]{0,})\/([A-z0-9]{0,})\/([A-z0-9]{0,})\/([A-z0-9]{0,})\//" , $_GET['params'], $params);
print_r($params);


:
. Apache Bench :
ab -c 10 -n 100000 test.com/param/val/param1/val/param2/val/param3/val


:
:

kasumi ~ # uname -a
Linux kasumi 2.6.24-tuxonice-r9 #1 SMP Sun Jul 13 02:57:59 Local time zone must be set--see zic i686 Intel(R) Pentium(R) M processor 1.60GHz GenuineIntel GNU/Linux

kasumi ~ # lshw
kasumi
description: Notebook
product: SX10P
vendor: Samsung Electronics
*-cpu
description: CPU
product: Intel(R) Pentium(R) M processor 1.60GHz
*-memory
description: System Memory
physical id: e
slot: System board or motherboard
size: 768MiB
*-disk
description: ATA Disk
product: HTS424040M9AT00
vendor: Hitachi
physical id: 0
bus info: ide@0.0
logical name: /dev/hda
version: MA2OA71A
serial: MPA241Q2DPTHJA
size: 37GiB (40GB)

kasumi ~ # apache2 -v
Server version: Apache/2.2.8 (Unix)
Server built: Jun 13 2008 20:05:00

kasumi ~ # php -v
PHP 5.2.6-pl7-gentoo (cli) (built: Oct 18 2008 13:21:57)
Copyright (c) 1997-2008 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2008 Zend Technologies


:

1. PHP Native:
Requests per second: 1570.26 [#/sec] (mean)
Time per request: 6.368 [ms] (mean)
Time per request: 0.637 [ms] (mean, across all concurrent requests)

2. Apache mod_rewrite:
Requests per second: 1579.41 [#/sec] (mean)
Time per request: 6.331 [ms] (mean)
Time per request: 0.633 [ms] (mean, across all concurrent requests)

3. PHP preg_match
Requests per second: 1528.33 [#/sec] (mean)
Time per request: 6.543 [ms] (mean)
Time per request: 0.654 [ms] (mean, across all concurrent requests)

chart

, , mod_rewrite , PHP.

Source: https://habr.com/ru/post/47650/


All Articles