News:

Wondering if this will always be free?  See why free is better.

Main Menu

Pretty URLs

Started by SMFHacks.com Team, January 31, 2007, 10:56:43 AM

Previous topic - Next topic

SirLouen

#7440
Which is the final solution to HTTPS switch with this mod?

EDIT:
Solved
1. Switch to HTTPS
2. Use repair_settings.php to change all links to HTTPS
3. Go to Database and change into "smf_settings" table the variable "pretty_root_url" to https://
4. Go to the Admin panel -> Forum Config -> Pretty URLS and disable Rewrite URL
5. Re-Enable Rewrite URL
6. Done!

vbgamer45

Glad you were able to figure it out.
Community Suite for SMF - Take your forum to the next level built for SMF, Gallery,Store,Classifieds,Downloads,more!

SMFHacks.com -  Paid Modifications for SMF

Mods:
EzPortal - Portal System for SMF
SMF Gallery Pro
SMF Store SMF Classifieds Ad Seller Pro

nend

Did a little rewriting with XPath, in no way it is truly ready as I need to add further support for anything that isn't HTML, say like XML.

<?php
// NEnd's custom Pretty URLs OB rewrite
// Based on Version 1.0 PrettyUrls-Filters.php

if (!defined('SMF'))
die('Hacking attempt...');

function 
xpath_rewrite_buffer($buffer) {

global $boardurl;

$doc = new DomDocument();
$doc->loadHTML($buffer);
$xpath = new DOMXPath($doc);
$search = array(
'a' => 'href',
'meta' => 'content',
'link' => 'href',
'form' => 'action',
);
foreach ($search as $tag => $attribute)
foreach ($xpath->query('//'.$tag.'[starts-with(@'.$attribute.', "'.$boardurl.'")]') as $node)
$node->setAttribute($attributexpath_urls_filter($node->getAttribute($attribute)));

return $doc->saveHTML();

/*
$doc->loadXML($buffer);
$xpath = new DOMXPath($doc);
$search = array('loc','link');
foreach ($search as $tag)
foreach ($xpath->query('//'.$tag.'') as $node)
$node->nodeValue = xpath_urls_filter($node->textContent);*/

}

// Rewrite the buffer with Pretty URLs!
function pretty_rewrite_buffer($buffer) {

global $boardurl$context;

/* if (!function_exists('ShowXmlFeed'))
return xpath_rewrite_buffer($buffer);*/

// Remove the script tags now
$context['pretty']['scriptID'] = 0;
$context['pretty']['scripts'] = array();
$buffer preg_replace_callback('~<script.+?</script>~s''pretty_scripts_remove'$buffer);

// Find all URLs in the buffer
$context['pretty']['search_patterns'][] = '~(<a[^>]+href=|<link[^>]+href=|<form[^>]+?action=|<meta property=\"og:url\" content=|<meta property=\"og:image\" content=|<meta property=\"og:video\" content=)(\"[^\"#]+|\'[^\'#]+)~';
$urls = array();
$context['pretty']['cached_urls'] = array();
foreach ($context['pretty']['search_patterns'] as $pattern) {
preg_match_all($pattern$buffer$matchesPREG_PATTERN_ORDER);
foreach ($matches[2] as $match) {
// Rip out everything that shouldn't be cached
$match preg_replace(array('~^[\"\']|PHPSESSID=[^;]+|(se)?sc=[^;]+|' $context['session_var'] . '=[^;]+~''~\"~''~;+|=;~''~\?;~''~\?$|;$|=$~'), array('''%22'';''?'''), $match);
// Absolutise relative URLs
if (!preg_match('~^[a-zA-Z]+:|^#|@~'$match) && SMF != 'SSI')
$match $boardurl '/' $match;
if (substr($match,0,7) == 'mailto:' || substr($match,0,10) == 'javascript' || !strstr($match'?'))
continue;
if (substr($match,0,strlen($boardurl)) != $boardurl || substr($match,strlen($boardurl),8) == '/Sources' || substr($match,strlen($boardurl),7) == '/Themes' || substr($match,strlen($boardurl),4) == '/img' || substr($match,strlen($boardurl),8) == '/Smileys' || substr($match,strlen($boardurl),8) == '/avatars')
continue;

$urls[$match] = array('url' => $match);
}
}

// Procede only if there are actually URLs in the page
if (count($urls) != 0)
pretty_urls_filter($urls);

// Put the URLs back into the buffer
$context['pretty']['replace_patterns'][] = '~(<a[^>]+href=|<link[^>]+href=|<form[^>]+?action=|<meta property=\"og:url\" content=|<meta property=\"og:image\" content=|<meta property=\"og:video\" content=)(\"[^\"]+\"|\'[^\']+\')~';
foreach ($context['pretty']['replace_patterns'] as $pattern)
$buffer preg_replace_callback($pattern'pretty_buffer_callback'$buffer);

// Restore the script tags
if ($context['pretty']['scriptID'] > 0)
$buffer preg_replace_callback("~\x14([0-9]+)\x14~"'pretty_scripts_restore'$buffer);

// Return the changed buffer.
return $buffer;
}

// Remove and save script tags
function pretty_scripts_remove($match) {
global $context;

$context['pretty']['scriptID']++;
$context['pretty']['scripts'][$context['pretty']['scriptID']] = $match[0];
return "\x14" $context['pretty']['scriptID'] . "\x14";
}

// A callback function to replace the buffer's URLs with their cached URLs
function pretty_buffer_callback($matches) {
global $boardurl$context;

// Is this URL in an attribute, and so will need new quotes?
$addQuotes preg_match('~^[\"\']~'$matches[2]);

// Remove those annoying quotes
$matches[2] = preg_replace('~^[\"\']|[\"\']$~'''$matches[2]);

// Store the parts of the URL that won't be cached so they can be inserted later
preg_match('~PHPSESSID=[^;#&]+~'$matches[2], $PHPSESSID);
preg_match('~(se)?sc=[^;#]+~'$matches[2], $sesc);
preg_match('~' $context['session_var'] . '=[^;#]+~'$matches[2], $session_var);
preg_match('~#.*~'$matches[2], $fragment);

// Rip out everything that won't have been cached
$url_id preg_replace(array('~PHPSESSID=[^;#]+|(se)?sc=[^;#]+|' $context['session_var'] . '=[^;#]+|#.*$~''~\"~''~;+|=;~''~\?;~''~\?$|;$|=$~'), array('''%22'';''?'''), $matches[2]);

// Absolutise relative URLs
if (!preg_match('~^[a-zA-Z]+:|@~'$url_id) && !($url_id == '' && isset($fragment[0])) && SMF != 'SSI')
$url_id $boardurl '/' $url_id;

// Stitch everything back together, clean it up and return
$replacement = isset($context['pretty']['cached_urls'][$url_id]) ? $context['pretty']['cached_urls'][$url_id] : $url_id;
$replacement .= (strpos($replacement'?') === false '?' ';') . (isset($PHPSESSID[0]) ? $PHPSESSID[0] : '') . ';' . (isset($sesc[0]) ? $sesc[0] : '') . (isset($session_var[0]) ? $session_var[0] : '') . (isset($fragment[0]) ? $fragment[0] : '');
$replacement preg_replace(array('~;+|=;~''~\?;~''~\?#|;#|=#~''~\?$|&amp;$|;$|#$|=$~'), array(';''?''#'''), $replacement);
$replacement str_replace('index.php'''$replacement);// Remove index.php, who needs it.
return $matches[1] . ($addQuotes '"' '') . $replacement . ($addQuotes '"' '');
}

// Put the script tags back
function pretty_scripts_restore($match) {
global $context;

return $context['pretty']['scripts'][(int) $match[1]];
}

// Filter URLs
function pretty_urls_filter($urls) {
global $boardurl$context$scripturl;

foreach ($urls as $url_id => $url) {
// Filter Aeva Media
if (strpos($url['url'], 'action=media') !== false){
$context['pretty']['cached_urls'][$url_id] = preg_replace(array('~.*[?;&]action=media;sa=media;in=([0-9]+);(thumba?|preview)(.*)~S','~.*[?;&]action=media;sa=(album|item|media);in=([0-9]+)(.*)~S','~.*[?;&]action=media(.*)~S'), array($boardurl '/media/$2/$1/?$3',$boardurl '/media/$1/$2/?$3',$boardurl '/media/?$1'), $url['url']);
continue;
}
//Filter actions
if (preg_match('`' $scripturl '(.*)action=([^;]+)(.*)`S'$url['url'], $matches)){
$context['pretty']['cached_urls'][$url_id] = $boardurl '/a/' $matches[2] . '/' $matches[1] . $matches[3];
// $context['pretty']['cached_urls'][$url_id] = preg_replace('`' . $scripturl . '(.*)action=([^;]+)`S', $boardurl . '/$2/$1', $url['url']);
continue;
}
//Filter topics
if (preg_match('`' $scripturl '(.*[?;&])topic=([.a-zA-Z0-9]+)(.*)`S'$url['url'], $matches)){
if (strpos($matches[2], '.') !== false)
list ($topic_id$start) = explode('.'$matches[2]);
else{
$topic_id $matches[2];
$start '0';
}
$topic_id = (int) $topic_id;
$start $start != '0' '?start='$start '';
$context['pretty']['cached_urls'][$url_id] = $boardurl '/t/' $topic_id '/' $start $matches[1] . $matches[3];
continue;
}
// Filter boards
if (preg_match('`' $scripturl '(.*[?;&])board=([.0-9]+)(.*)`S'$url['url'], $matches)){
if (strpos($matches[2], '.') !== false)
list ($board_id$start) = explode('.'$matches[2]);
else{
$board_id $matches[2];
$start '0';
}
$board_id = (int) $board_id;
$start $start != '0' $start '/' '';
$context['pretty']['cached_urls'][$url_id] = $boardurl '/b/' $board_id '/' $start $matches[1] . $matches[3];
continue;
}
// None
$context['pretty']['cached_urls'][$url_id] = preg_replace(array('~\"~''~;+|=;~''~\?;~''~\?$|;$|=$~'), array('%22'';''?'''),str_replace("\x12"'\''$url['url']));
}
}

function 
xpath_urls_filter($url) {

global $boardurl$scripturl;

if (strpos($url'?') == false)
return xpath_url_clean($url);

if (strpos($url'action=media') !== false)
return(xpath_url_clean(preg_replace(array('~.*[?;&]action=media;sa=media;in=([0-9]+);(thumba?|preview)(.*)~S','~.*[?;&]action=media;sa=(album|item|media);in=([0-9]+)(.*)~S','~.*[?;&]action=media(.*)~S'), array($boardurl '/media/$2/$1/?$3',$boardurl '/media/$1/$2/?$3',$boardurl '/media/?$1'), $url)));
if (strpos($url'action=') !== false)
return(xpath_url_clean(preg_replace('`' $scripturl '(.*)action=([^;]+)(.*)`S',$boardurl '/a/$2/$1$3',$url)));
if (strpos($url'topic=') !== false && preg_match('`' $scripturl '(.*[?;&])topic=([.a-zA-Z0-9]+)(.*)`S'$url$matches)){
if (strpos($matches[2], '.') !== false)
list ($topic_id$start) = explode('.'$matches[2]);
else
$topic_id $matches[2];

return(xpath_url_clean($boardurl '/t/' . (int) $topic_id '/' .(!empty($start) ? '?start='.$start ''). $matches[1] . $matches[3]));
}
if (strpos($url'board=') !== false && preg_match('`' $scripturl '(.*[?;&])board=([.0-9]+)(.*)`S'$url$matches)){
if (strpos($matches[2], '.') !== false)
list ($board_id$start) = explode('.'$matches[2]);
else
$board_id $matches[2];

return(xpath_url_clean($boardurl '/b/' . (int) $board_id '/' .(!empty($start) ? $start.'/' ''). $matches[1] . $matches[3]));
}
return xpath_url_clean($url);
}

function 
xpath_url_clean($url){
$url preg_replace(array('~;+|=;~''~\?;~''~\?#|;#|=#~''~\?$|&amp;$|;$|#$|=$~'), array(';''?''#'''), $url);
$url str_replace('index.php'''$url);
return $url;
}
?>



Thought I would share that just in case you want to improve upon it.  ;)

nend

Just finished my rewrite today with XML support. The code base now almost shares nothing besides a few search strings with Pretty URLs. The other Pretty URL files where canned, the one for the admin panel and rewriting boards to names. The source code edits by the mod were revised and edited accordingly and also there are no more database calls.

<?php
// NEnd's custom Pretty URLs OB rewrite
// Based on Version 1.0 PrettyUrls-Filters.php

if (!defined('SMF'))
die('Hacking attempt...');

function 
xpath_rewrite_buffer($buffer) {

global $boardurl;

if (strpos($buffer,'<!DOCTYPE') !== false) {
$doc = new DomDocument();
$doc->loadHTML($buffer);
$xpath = new DOMXPath($doc);
$search = array(
'a' => 'href',
'meta' => 'content',
'link' => 'href',
'form' => 'action'
);
foreach ($search as $tag => $attribute)
foreach ($xpath->query('//'.$tag.'[starts-with(@'.$attribute.', "'.$boardurl.'")]') as $node)
$node->setAttribute($attributexpath_urls_filter($node->getAttribute($attribute)));
$buffer $doc->saveHTML();
} else if (strpos($buffer,'<?xml') !== false) {
$doc = new DomDocument();
$doc->loadXML($buffer);
$xpath = new DOMXPath($doc);
$search = array('loc','link','url','guid');
foreach ($search as $tag)
foreach ($xpath->query('//'.$tag.'[starts-with(text(),"'.$boardurl.'")]') as $node)
$node->nodeValue xpath_urls_filter($node->nodeValue);
$search = array(
'enclosure' => 'url'
);
foreach ($search as $tag => $attribute)
foreach ($xpath->query('//'.$tag.'[starts-with(@'.$attribute.', "'.$boardurl.'")]') as $node)
$node->setAttribute($attributexpath_urls_filter($node->getAttribute($attribute)));
$buffer $doc->saveXML();
}
return $buffer;
}

// Rewrite the buffer with Pretty URLs!
function pretty_rewrite_buffer($buffer) {
return xpath_rewrite_buffer($buffer);
}

function 
xpath_urls_filter($url) {

global $boardurl$scripturl;

if (strpos($url'?') == false)
return xpath_url_clean($url);

if (strpos($url'action=media') !== false)
return(xpath_url_clean(preg_replace(array('~.*[?;&]action=media;sa=media;in=([0-9]+);(thumba?|preview)(.*)~S','~.*[?;&]action=media;sa=(album|item|media);in=([0-9]+)(.*)~S','~.*[?;&]action=media(.*)~S'), array($boardurl '/media/$2/$1/?$3',$boardurl '/media/$1/$2/?$3',$boardurl '/media/?$1'), $url)));
if (strpos($url'action=') !== false)
return(xpath_url_clean(preg_replace('`' $scripturl '(.*)action=([^;]+)(.*)`S',$boardurl '/a/$2/$1$3',$url)));
if (strpos($url'topic=') !== false && preg_match('`' $scripturl '(.*[?;&])topic=([.a-zA-Z0-9]+)(.*)`S'$url$matches)){
if (strpos($matches[2], '.') !== false)
list ($topic_id$start) = explode('.'$matches[2]);
else
$topic_id $matches[2];

return(xpath_url_clean($boardurl '/t/' . (int) $topic_id '/' .(!empty($start) ? '?start='.$start ''). $matches[1] . $matches[3]));
}
if (strpos($url'board=') !== false && preg_match('`' $scripturl '(.*[?;&])board=([.0-9]+)(.*)`S'$url$matches)){
if (strpos($matches[2], '.') !== false)
list ($board_id$start) = explode('.'$matches[2]);
else
$board_id $matches[2];

return(xpath_url_clean($boardurl '/b/' . (int) $board_id '/' .(!empty($start) ? $start.'/' ''). $matches[1] . $matches[3]));
}
return xpath_url_clean($url);
}

function 
xpath_url_clean($url){
$url preg_replace(array('~;+|=;~''~\?;~''~\?#|;#|=#~''~\?$|&amp;$|;$|#$|=$~'), array(';''?''#'''), $url);
$url str_replace('index.php'''$url);
return $url;
}
?>


I am now using it live on my site to test to see the performance.

I hope you can use this in the future, however this code requires PHP 5 and above.

vbgamer45

Trying to learn more what would this be used for. I have used XPath with xml
Community Suite for SMF - Take your forum to the next level built for SMF, Gallery,Store,Classifieds,Downloads,more!

SMFHacks.com -  Paid Modifications for SMF

Mods:
EzPortal - Portal System for SMF
SMF Gallery Pro
SMF Store SMF Classifieds Ad Seller Pro

nend

Quote from: vbgamer45 on September 17, 2016, 04:04:38 PM
Trying to learn more what would this be used for. I have used XPath with xml

I am using it for URL rewriting, however it does have some drawbacks. Say for instance if there are any errors in the markup when you pass the buffer through XPath it will try to correct the mistakes sometimes resulting in some minor problems with the HTML output.

The only way to prevent this problem is to make sure the HTML is correct and error free. This though would make support for something like this a nightmare so even though it has its performance gains I wouldn't recommend making it into a modification. Besides anyone that understands how to use it would know how to use the code I posted for their own means.

Tiribulus

What if somebody has a domain for a while and registers a second one, both resolving to the same host. Pretty urls insists upon rewriting the addresses to the first name which I'm trying to phase out. How would I force it to rewrite them to the new one instead? Any help would be greatly appreciated.
Thanks

vbgamer45

Community Suite for SMF - Take your forum to the next level built for SMF, Gallery,Store,Classifieds,Downloads,more!

SMFHacks.com -  Paid Modifications for SMF

Mods:
EzPortal - Portal System for SMF
SMF Gallery Pro
SMF Store SMF Classifieds Ad Seller Pro

Tiribulus

Thank you vbgamer for the response, but I don't understand.

Where is the "pretty_root_url setting " that I can fix myself and the .php file I made with that code opens as a blank white page.

I don't mean to be difficult. I really don't understand.

Thanks again,

vbgamer45

It is in the database in the smf_settings table

That file will be blank when ran.
Community Suite for SMF - Take your forum to the next level built for SMF, Gallery,Store,Classifieds,Downloads,more!

SMFHacks.com -  Paid Modifications for SMF

Mods:
EzPortal - Portal System for SMF
SMF Gallery Pro
SMF Store SMF Classifieds Ad Seller Pro

Tiribulus

OUTSTANDING!!

Thank you sir. It is fixed.  :)

vbgamer45

Community Suite for SMF - Take your forum to the next level built for SMF, Gallery,Store,Classifieds,Downloads,more!

SMFHacks.com -  Paid Modifications for SMF

Mods:
EzPortal - Portal System for SMF
SMF Gallery Pro
SMF Store SMF Classifieds Ad Seller Pro

Tiribulus

It did indeed. Thanks again. I didn't think to look in that table at all.

I was around here a lot several years back. You've been here a while man :) A few others too I recognize.

Xpresskonami

Since I change my domain from old to new domain. I make use of 301 redirect and my forum is working well. but the main problem am facing is my pretty url. this has be giving me many errors in my google webmaster tools. Some of my url change to this


http://url.com/index.php?pretty%3bboard=android-app-board%3btopic=download-android-app-version-1-0.0


Instead of the url to be in this format.

http://url.com/android-app-board/download-android-app-version-1-0/


But if I un install the pretty url mods. everything work perfectly.
please how can I fix that.

Xpresskonami

Anyone should reply on how to fix it.

Nedano

I have a problem.

Pretty URLs works nice in computer but in mobile phone users can't register to my forum because when they hit "register" button they go to site "not found, the requested URL /register/index.php was not found on this server".

Register works in adress mydomain.com/register [nofollow] also with mobile phones.

So how can I remove that index.php from mobile register page?

vbgamer45

Any mods installed or mobile themes?
Community Suite for SMF - Take your forum to the next level built for SMF, Gallery,Store,Classifieds,Downloads,more!

SMFHacks.com -  Paid Modifications for SMF

Mods:
EzPortal - Portal System for SMF
SMF Gallery Pro
SMF Store SMF Classifieds Ad Seller Pro

Nedano

#7457
Quote from: vbgamer45 on October 20, 2016, 11:37:28 AM
Any mods installed or mobile themes?

Yes these mods:

1.    SEO Sitemap    2.2.1
2.    Responsive Curve    1.0.0 (this is the mobile theme)
3.    Ohara YouTube Embed    1.2.7    
4.    Remove Help From Menu    1.0    
5.    Google Tagged    2.0    
6.    SMF 2.0.12 Update    1.0    
7.    Board Viewers Mod    1.2.1.1b    
8.    SA Facebook    3.1    
9.    Optimus    1.9    
10.  Pretty URLs

I tried to removed Pretty URLs mod and then I was able to register with mobile phone (but that site url wasn't pretty ofcourse). Then I installed Pretty URLs again and got that ("not found..") information page again so guilty is Pretty URLs. I wanna get it work in adress mydomain.com/register [nofollow] because SEO.

vbgamer45

Try it without Optimus   
Pretty urls shouldn't add index.php to end of a url...
Community Suite for SMF - Take your forum to the next level built for SMF, Gallery,Store,Classifieds,Downloads,more!

SMFHacks.com -  Paid Modifications for SMF

Mods:
EzPortal - Portal System for SMF
SMF Gallery Pro
SMF Store SMF Classifieds Ad Seller Pro

Nedano

Where do I find the "smf_settings" table?

I don't have that in my phpmyadmin/mysql.

There is only some "hs5c_settings" but not "smf_settings".

Advertisement: