.htaccess rewrite to make static URLs

The 2.0.x discussion forum has been locked; this will remain read-only. The 3.0.x discussion forum has been renamed phpBB Discussion.

.htaccess rewrite to make static URLs

Postby majjk99 » Tue Dec 14, 2004 5:31 pm

I have been spending quite some time now searching for a mod or simple solution that uses .htaccess rewrite to make static URLs. However, every "solution" I've seen so far has been crippled by bugs. Is there a mod for this that WORKS? (I'm using phpBB 2.0.11) I think I have at least once in the past seen a solution for this, but I cannot find it again.
majjk99
Registered User
 
Posts: 233
Joined: Sun Dec 07, 2003 10:36 pm
Location: EU

Postby majjk99 » Sun Dec 19, 2004 9:36 am

Finally something that works! I found the solution at Webmaster Brain, where this mod has also been implemented on the forum. It looks like this:

*********************************

Open: 'includes/page_header.php'

Find:
Code:

$template->set_filenames(array('overall_header' => ( empty($gen_simple_header) ) ? 'overall_header.tpl' : 'simple_header.tpl'));

After the above code, add the below:
Code:

*********************************
ob_start();

function make_url_friendly($url)
{

$url = strtolower($url);

$find = array(' ','&','\r\n','\n','+');

$url = str_replace ($find, '-', $url);

$find = array('/[^a-z0-9\-<>]/','/[\-]+/','/<[^>]*>/');

$repl = array('','-','');

$url = preg_replace ($find, $repl, $url);

return $url;

}

function rewrite_urls($content)
{

function if_query($amp)
{

if($amp != '')
{
return '?';
}

}

$url_in = array('/(?<!\/)viewforum.php\?f=([0-9]+)((&amp;)|(&)){0,1}([^>]+>)(.*?)<\/a>/e','/(?<!\/)viewtopic.php\?t=([0-9]+)((&amp;)|(&)){0,1}([^>]+>)(.*?)<\/a>/e');

$url_out = array("make_url_friendly('\\6') . '-vf\\1.php' . if_query('\\2') . stripslashes('\\5\\6') . '</a>'","make_url_friendly('\\6') . '-vt\\1.php' . if_query('\\2') . stripslashes('\\5\\6') . '</a>'");

$content = preg_replace($url_in, $url_out, $content);

return $content;

}
*********************************

Note: Unless you have a very high-res screen setting this code will get wrapped

Open: includes/page_tail.php

Find:
Code:

$db->sql_close();

After the above, add:
Code:

*********************************
$contents = ob_get_contents();
ob_end_clean();

echo rewrite_urls($contents);

global $dbg_starttime;
*********************************

If you are using GZIP compression:

Find:
Code:

$gzip_contents = ob_get_contents();
ob_end_clean();

After, add:
Code:

*********************************
echo rewrite_urls($contents);
global $dbg_starttime;
*********************************

Now create a .htaccess file in the directory containing your phpBB installation (if it's not already there), add/edit the following into it:

Code:

*********************************
Options +FollowSymLinks
DirectoryIndex index.php
RewriteEngine On
RewriteBase /forum
RewriteRule [.]*-vf([0-9]*) viewforum.php?%{QUERY_STRING}&f=$1
RewriteRule [.]*-vt([0-9]*) viewtopic.php?%{QUERY_STRING}&t=$1
*********************************

The only bit I had to change was the "RewriteBase /forum" in the .htaccess file. change "forum" to whatever your forum directory is called, or use only "/" if it is located in the root folder.

[Please let me know if you have used this and improved it somehow. There are at least three things that could be improved: 1) If you click member list and find a member and choose a post by that member, you won't get the right page name. 2) Are there more han 15 posts, then you get more than 1 page. URL for first page is as ok. If you click second page you get an URL that isn't ok. 3) "View previous topic" and "View next topic" give you names like .../view-previous-topic-vt471.php?view=previous, i.e. 1 page - several page names.]
majjk99
Registered User
 
Posts: 233
Joined: Sun Dec 07, 2003 10:36 pm
Location: EU

Postby NeoThermic » Sun Dec 19, 2004 10:19 am

The one I use has a slightly more complicated set of rules.

First, the page_header.php file:


Find:
Code: Select all
//
// Generate logged in/logged out status
//


After, add:
Code: Select all
ob_start();

function replace_for_mod_rewrite(&$s)
{
$urlin =
array(
"'(?<!/)viewforum.php\?f=([0-9]*)&(?:amp;)topicdays=([0-9]*)&(?:amp;)start=([0-9]*)'",
"'(?<!/)viewforum.php\?f=([0-9]*)&(?:amp;)mark=topics'",
"'(?<!/)viewforum.php\?f=([0-9]*)'",
"'(?<!/)viewtopic.php\?t=([0-9]*)&(?:amp;)view=previous'",
"'(?<!/)viewtopic.php\?t=([0-9]*)&(?:amp;)view=next'",
"'(?<!/)viewtopic.php\?t=([0-9]*)&(?:amp;)postdays=([0-9]*)&(?:amp;)postorder=([a-zA-Z]*)&(?:amp;)start=([0-9]*)'",
"'(?<!/)viewtopic.php\?t=([0-9]*)&(?:amp;)start=([0-9]*)&(?:amp;)postdays=([0-9]*)&(?:amp;)postorder=([a-zA-Z]*)&(?:amp;)highlight=([a-zA-Z0-9]*)'",
"'(?<!/)viewtopic.php\?t=([0-9]*)&(?:amp;)start=([0-9]*)'",
"'(?<!/)viewtopic.php\?t=([0-9]*)'",
"'(?<!/)viewtopic.php&(?:amp;)p=([0-9]*)'",
"'(?<!/)viewtopic.php\?p=([0-9]*)'",
);
$urlout = array(
"viewforum\\1-\\2-\\3.html",
"forum\\1.html",
"forum\\1.html",
"ptopic\\1.html",
"ntopic\\1.html",
"ftopic\\1-\\2-\\3-\\4.html",
"ftopic\\1.html",
"ftopic\\1-\\2.html",
"ftopic\\1.html",
"sutra\\1.html",
"sutra\\1.html",
);
$s = preg_replace($urlin, $urlout, $s);
return $s;
}


Then, page_tail.php:

Find:
Code: Select all
$db->sql_close();



After, Add:
Code: Select all
$contents = ob_get_contents();
ob_end_clean();
echo replace_for_mod_rewrite($contents);


Then heres the best bit. The rewrite rules:

Code: Select all
RewriteEngine On
RewriteRule ^forums.* index.php
RewriteRule ^forum([0-9]*).* viewforum.php?f=$1&mark=topic
RewriteRule ^viewforum([0-9]*)-([0-9]*)-([0-9]*).* viewforum.php?f=$1&topicdays=$2&start=$3
RewriteRule ^forum([0-9]*).* viewforum.php?f=$1
RewriteRule ^ptopic([0-9]*).* viewtopic.php?t=$1&view=previous
RewriteRule ^ntopic([0-9]*).* viewtopic.php?t=$1&view=next
RewriteRule ^ftopic([0-9]*)-([0-9]*)-([a-zA-Z]*)-([0-9]*).* viewtopic.php?t=$1&postdays=$2&postorder=$3&start=$4
RewriteRule ^ftopic([0-9]*)-([0-9]*).* viewtopic.php?t=$1&start=$2
RewriteRule ^ftopic([0-9]*).* viewtopic.php?t=$1
RewriteRule ^ftopic([0-9]*).html viewtopic.php?t=$1&start=$2&postdays=$3&postorder=$4&highlight=$5
RewriteRule ^sutra([0-9]*).* viewtopic.php?p=$1


That gets it all right, including second pages and previous/next links. The reason yours wasn't working as expected was due to the fact that if your seperator character is & rather than &amp;, it won't get interprted.

NeoThermic

Edit: added ; after ob_start(), spotted by next poster :)
Last edited by NeoThermic on Sun Feb 20, 2005 6:35 pm, edited 1 time in total.
NeoThermic.com... a well of information. Ask me for the bit bucket so you can drink its goodness. ||新熱です
User avatar
NeoThermic
Styles Team Member
Styles Team Member
 
Posts: 2141
Joined: Thu Dec 25, 2003 1:33 am
Location: United Kingdom

Postby majjk99 » Sun Feb 20, 2005 6:22 pm

Thanks,
I've decided to use yours as well. In case someone else want to try this out, don't forget to add a semicolon after "ob_start()" in the very beginning... otherwise you'll end up with parse error.
majjk99
Registered User
 
Posts: 233
Joined: Sun Dec 07, 2003 10:36 pm
Location: EU

Postby alcaeus » Sun Feb 20, 2005 8:07 pm

Well, I have started work on a MOD similar to yours, which also includes the title of a topic in the topic URL. It is not yet completed as there are some issues that need to be fixed.
However, you might want to check it out, maybe it helps: http://alcaeus.gigl.org/alcaeus/viewtopic.php?t=67

Greetz
alcaeus
alcaeus
I've Been Banned!
 
Posts: 431
Joined: Wed Nov 19, 2003 1:12 pm

Postby majjk99 » Sun Feb 20, 2005 11:01 pm

Are you using it yourself yet, I mean, is it possible to see what the result might look like? What issues are there to be fixed?
majjk99
Registered User
 
Posts: 233
Joined: Sun Dec 07, 2003 10:36 pm
Location: EU

Postby alcaeus » Mon Feb 21, 2005 7:28 am

majjk99 wrote:Are you using it yourself yet, I mean, is it possible to see what the result might look like?

No, I'm not. I made the MOD before I realized that my hoster doesn't allow me to set rewrite rules :roll:
However, there is another forum where I got the idea from, it's german, but you'll get the idea of what it will look like: www.delphipraxis.net
As an example: the URL for this topic rewritten by my MOD would be:
Code: Select all
www.phpbb.com/phpBB/topic247077_htaccess_rewrite_to_make_static_URLs.html


What issues are there to be fixed?

The reason why it was denied (back in November, I haven't had any time to release a fixed version) is, that ob_start is not available on all versions of php, therefore I should find a workaround. Also, I'm not really sure if it works on all combinations of php-Versions and gzip compression enabled, but if you have gzip compression disabled you'll have no problem.

Greetz
alcaeus

[add]
If you run into any problems with the MOD whatsoever, remember to ask the question in my support forum, and not here, since there is no discussion thread for the MOD as it is not released ;)
[/add]
alcaeus
I've Been Banned!
 
Posts: 431
Joined: Wed Nov 19, 2003 1:12 pm

Postby hp_solomon » Thu Sep 15, 2005 2:50 pm

I had posted this to other topics but I cant find answers.

Can someone help me with the re_writing my forum? I want it this way:

From:
http://www.domain.com/viewforum.php?f=22

Forum Category Name : Bands

To:
http://www.bands.domain.com

If this is not possible, example below is okay

http://www.domain.com/bands (without the file extension)


I hope someone can help me with this. Thanks.
hp_solomon
Registered User
 
Posts: 269
Joined: Wed Mar 30, 2005 8:06 am


Return to 2.0.x Discussion

Who is online

Users browsing this forum: No registered users and 5 guests