Forum Moderators: phranque
id int(11) auto_increment primary key
rid int(11)
type varchar(10)
alias varchar(128)
Request: http://example.com/index.php?param=4
After rewrite: http://example.com/article/tidy-article-title
Request: http://example.com/index.php?param=4
After rewrite: http://example.com/index.php?param=4
init rewrite engine with requested uri /index.php
applying pattern '^[/index.php]+\?([a-zA-Z0-9]+)=([a-zA-Z0-9]+)$' to uri '/index.php'
pass through /index.php
[perdir /var/www/html/] pass through /var/www/html/index.php
init rewrite engine with requested uri /index.php
applying pattern '^[/index.php]+\?([a-zA-Z0-9]+)=([a-zA-Z0-9]+)$' to uri '/index.php'
pass through /index.php
[perdir /var/www/html/] pass through /var/www/html/index.php
rewrite engine with requested uri /style.css
applying pattern '^[/index.php]+\?([a-zA-Z0-9]+)=([a-zA-Z0-9]+)$' to uri '/style.css'
pass through /style.css
[perdir /var/www/html/] pass through /var/www/html/style.css
init rewrite engine with requested uri /scripts.js
applying pattern '^[/index.php]+\?([a-zA-Z0-9]+)=([a-zA-Z0-9]+)$' to uri '/scripts.js'
pass through /scripts.js
[perdir /var/www/html/] pass through /var/www/html/scripts.js
RewriteEngine on
RewriteLogLevel 9
RewriteLog "/var/log/rewrite.log"
RewriteMap newurl prg://var/www/cgi-bin/cleanurls.php
RewriteCond %{QUERY_STRING} ^([a-zA-z0-9]+)=([a-zA-Z0-9]+)$
RewriteRule ^[/index.php]+\?([a-zA-Z0-9]+)=([a-zA-Z0-9]+)$ $(newurl:$1/$2)? [L]
#!/usr/bin/php
<?php
// Connect (as root) to MySQL database containing aliases
include 'mysql_database_connect.php';
// So it doesn't crap out prematurely
set_time_limit(0);
// Grab STDIN (request being made of the server)
$keyboard = fopen("php://stdin","r");
while (1) {
// Read STDIN to variable
$line = fgets($keyboard);
// Match elements to use for db query
if (preg_match('\?([a-zA-Z0-9]+)=([a-zA-Z0-9]+)$/', $line, $igot)) {
// Grab the alias (i.e. 'tidy-article-title')
$getalias = mysql_query("select alias, type from url_alias where rid = '$igot[2]'");
while($row=mysql_fetch_array($getalias)) {
// Print clean alias to STDOUT (back to the "address bar")
print $row['type'] . "/" . $row['alias'] . "\n";
}
}
else {
// Catchall ... should never get here because of the RewriteCond ...
print "$line\n";
}
}
RewriteEngine on
RewriteLogLevel 9
RewriteLog "/var/log/rewrite.log"
RewriteMap newurl prg://var/www/cgi-bin/cleanurls.php
RewriteCond %{QUERY_STRING} ^([a-z0-9]+)=([a-z0-9]+)$ [NC]
RewriteRule ^/(index\.php)?$ ${newurl:%1/%2}? [L]
RewriteRule ^/(index\.php)?$ http://www.example.com/${newurl:%1/%2}? [R=301,L]
id int(11) primary key auto_increment
rid int(11)
type varchar(10)
alias varchar(128)
id = 11;
rid = 11;
type = 'article';
alias = 'this-is-an-article';
# start mod_rewrite
RewriteEngine on
# find processing script here
RewriteMap newurl prg://var/www/cgi-bin/cleanurls.php
# enable lock file while rewriting (protection against collisions)
RewriteLock /var/lock/map.newurl.lock
# if RewriteRule matches, check for specific case
RewriteCond %{REQUEST_URI} ^/article.*
# any URI matches, and is then tested by the RewriteCond, above
RewriteRule ^/(.*) ${newurl:$1} [L]
#!/usr/bin/php
<?php
# PHP/MySQL db connection
include '/path/to/db_connect.php';
# this program cannot die ... TO DO: graceful error handling needed
set_time_limit(0);
# assign STDIN to handler
$keyboard = fopen("php://stdin","r");
# always
while (1) {
# read STDIN to variable from handler
$line = fgets($keyboard);
# check for string '/chars/chars/' in URI
if (preg_match('/(.*)\/(.*)/', $line, $igot)) {
# grab pieces for resolving dynamic resource from db table ('article' and '11')
# matches 'alias' string, so that must be unique!
$getalias = mysql_query("select type, rid from url_alias where type = '$igot[1]' && alias = '$igot[2]'");
while($row=mysql_fetch_array($getalias)) {
$atype = $row['type'];
$arid = $row['rid'];
}
# print dynamic resource reference to STDOUT (does not refresh URI in address bar)
print "/index.php?$atype=$arid\n";
}
else {
# did not match '/chars/chars/', so just use the original URI (i.e. 'about.html')
print "$line\n";
}
}
?>
<link rel="stylesheet" type="text/css" href="style.css" />
<script type="text/javascript" src="scripts.js"></script>
<link rel="stylesheet" type="text/css" href="/style.css" />
<script type="text/javascript" src="/scripts.js"></script>
(1) take the current page's URL, (2) strip off the page part of that URL, back to the final slash in the URL and then (3) append the relative reference on the end.
(1) http://www.example.com/index.php
(2) http://www.example.com/
(3) http://www.example.com/style.css
Or are you saying
(2) http://www.example.com with no trailing slash,
which is where the relative 'style.css' got lost (http://www.example.comstyle.css)?\
The rewritten path BECOMES the web path, for the browser. The name, location and other identifying features of the resource being displayed is irrelevant to the browser ... it eats what the server feeds it.
The server fed it "/article/blah-blah", not "/index.php?p=123", therefore a request for "style.css" would be relative to the "/article" directory the browser "thought" it was in.
By using a server-relative link <img src="/img.src">, you tell the browser to remove both the page name and all subdirectory path-info from the page URL, and then append the "/img.src", making the image URL "example.com/img.src".
../ syntax. You'll drive yourself nuts trying to figure out what is relative to what. /var/www/yoursite/media/images/ folder on your server, just call that image using "/media/images/thatimage.png" with a leading slash when you link to it - then it doesn't matter "where" you are linking from.