Php, regex, mod_rewrite tips

Ciao! E’ la prima volta che ti vedo qua, se vuoi seguirmi sottoscrivi il feed RSS.
If you’re new here, you may want to subscribe to my RSS feed. Thanks for visiting!

In questi giorni si lavora parecchio sullo sviluppo di sistemi web di un certo tipo, il lavoro si concentra sia su php che su Apache e ho scoperto alcuni siti notevoli per problematiche specifiche che vorrei segnalare a tutti gli interessati.

Prima di tutto avevo bisogno di parsare un file di testo alla ricerca di URL validi da trasformare in link HTML (del tipo ogni volta che incontri qualcosa come “http://www.michelem.org/linux” (apici esclusi ovviamente) me lo trasformi in “<a href=’http://www.michelem.org/linux ‘>http://www.michelem.org/linux</a>“), e grazie al mitico motore di ricerca per regex “Regular Expression Library” ho trovato quello che andava bene al mio caso, vi lascio due appunti.

How to parse a text file and include it in some page with URL converted in hyperlinks (href):
function hyperlink($text)
{
$text = ereg_replace(”(mailto\:|(news|(ht|f)tp(s?))\://)(([^[:space:]]+)|([^[:space:]]+)( #([^#]+)#)?)”, “<a href=\”\\0\”>\\0</a>”, $text);
return $text;
}

$handle = fopen(”file.txt”, “r”);
while (!feof($handle)) {
$contents = fgets($handle, 4096);
//We want all symbols converted in html
$contents = htmlentities(htmlspecialchars($contents)).”<br>\n”;
//Now we can add hyperlinks to each single line and print it
print hyperlink($contents);
}

Seconda cosa che mi serviva era quella di convertire i link di un sito in qualcosa di piu’ intuibile e soprattutto indicizzabile dai motori. In pratica la stessa cosa che avviene con Wordpress e i permalinks (ne ho gia’ parlato qui).
Quindi se avevo un url del tipo: http://www.domain.com/index.php?c=category&p=productname volevo che comparisse all’utente e ai motori come http://www.domain.com/category/productname.
Vediamo come fare utilizzando il modulo mod_rewrite di Apache:

How to convert your site url in something more search and user friendly:

First of all keep in mind you have to be able to use htaccess file in your Apache virtual host.

Convert url like this:
http://www.domain.com/index.php?c=category&p=productname
in something like this:
http://www.domain.com/category/productname/

This is your .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)/$ /index.php?c=$1&p=$2
</IfModule>

You can notice the two variables $1 and $2 which are associated with the two regex “([^/]+)”.
Obviously you have to change your php script to reflect these new links in your pages.

More information on:
mod_rewrite: A Beginner’s Guide to URL Rewriting
Module Rewrite - URL Rewriting Guide

Post simili:

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

No Responses to “Php, regex, mod_rewrite tips”

No comments yet.

Leave a comment

(required)

(required)



Comments links could be nofollow free.

Categories