Rewrite rule to caddy 2 for Gnuboard 5.4

I often use gnuboard which is used a lot in Korea.
It is a situation where rewrite is required to make a long address short.
Here are the rewrite rules for apache and for nginx.

#### gnuboard5 apache rewrite BEGIN #####
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^content/([0-9a-zA-Z_]+)$  bbs/content.php?co_id=$1&rewrite=1  [QSA,L]
RewriteRule ^content/([^/]+)/$  bbs/content.php?co_seo_title=$1&rewrite=1      [QSA,L]
RewriteRule ^rss/([0-9a-zA-Z_]+)$  bbs/rss.php?bo_table=$1        [QSA,L]
RewriteRule ^([0-9a-zA-Z_]+)$  bbs/board.php?bo_table=$1&rewrite=1      [QSA,L]
RewriteRule ^([0-9a-zA-Z_]+)/([^/]+)/$ bbs/board.php?bo_table=$1&wr_seo_title=$2&rewrite=1      [QSA,L]
RewriteRule ^([0-9a-zA-Z_]+)/write$  bbs/write.php?bo_table=$1&rewrite=1    [QSA,L]
RewriteRule ^([0-9a-zA-Z_]+)/([0-9]+)$  bbs/board.php?bo_table=$1&wr_id=$2&rewrite=1  [QSA,L]
</IfModule>
#### gnuboard5 rewrite END #####
#### gnuboard5 nginx rules BEGIN #####
if (!-e $request_filename){
rewrite ^/content/([0-9a-zA-Z_]+)$ /bbs/content.php?co_id=$1&rewrite=1 break;
rewrite ^/content/([^/]+)/$ /bbs/content.php?co_seo_title=$1&rewrite=1 break;
rewrite ^/rss/([0-9a-zA-Z_]+)$ /bbs/rss.php?bo_table=$1 break;
rewrite ^/([0-9a-zA-Z_]+)$ /bbs/board.php?bo_table=$1&rewrite=1 break;
rewrite ^/([0-9a-zA-Z_]+)/write$ /bbs/write.php?bo_table=$1&rewrite=1 break;
rewrite ^/([0-9a-zA-Z_]+)/([^/]+)/$ /bbs/board.php?bo_table=$1&wr_seo_title=$2&rewrite=1 break;
rewrite ^/([0-9a-zA-Z_]+)/([0-9]+)$ /bbs/board.php?bo_table=$1&wr_id=$2&rewrite=1 break;
}
#### gnuboard5 nginx rules END #####

Caddy 2 for gnuboard rewrite rule

@content path_regexp content ^/content/([0-9a-zA-Z_]+)$
rewrite @content /bbs/content.php?co_id={re.content.1}&rewrite=1
@title path_regexp title ^/content/([^/]+)/$
rewrite @title /bbs/content.php?co_seo_title={re.title.1}&rewrite=1
@rss path_regexp rss ^rss/([0-9a-zA-Z_]+)$
rewrite @rss /bbs/rss.php?bo_table={re.rss.1}
@board path_regexp board ^/([0-9a-zA-Z_]+)$
rewrite @board /bbs/board.php?bo_table={re.board.1}&rewrite=1
@write path_regexp write ^/([0-9a-zA-Z_]+)/write$
rewrite @write /bbs/write.php?bo_table={re.write.1}&{query}
@seo path_regexp seo ^/([0-9a-zA-Z_]+)/([^/]+)/$
rewrite @seo /bbs/board.php?bo_table={re.seo.1}&wr_seo_title={re.seo.2}&rewrite=1
@id path_regexp id ^/([0-9a-zA-Z_]+)/([0-9]+)$
rewrite @id /bbs/board.php?bo_table={re.id.1}&wr_id={re.id.2}&rewrite=1

Hi, thanks for letting us know your use for Caddy!

What have you tried so far?

If you’re using the Caddyfile, the rewrite directive docs are here: https://caddyserver.com/docs/caddyfile/directives/rewrite

For the conditions, you can use matchers: https://caddyserver.com/docs/caddyfile/matchers

1 Like

I read all the links you gave me. But since I am a beginner in the web, it is almost impossible to fully understand. So I haven’t even figured out how to start. If you give me some examples, I’ll try to fix it.

This is the general idea:

@content path_regexp content ^content/([0-9a-zA-Z_]+)$
rewrite @content /bbs/content.php?co_id={re.content.1}&rewrite=1

So you have a named matcher @content which defines a regular expression on the request path, and using content as the name for the regexp so the result can be used later.

Then we have a rewrite that only applies if the @content matcher matches which rewrites the path using the regexp result with a placeholder {re.content.1} where content is the regexp name from before, and 1 is the index of the match group in the regexp (the stuff wrapped in () parentheses).

I think you should be able to figure out the rest. Feel free to follow up with more specific questions!

2 Likes

thx! I’ll try it!

One note I realized - Caddy has its paths typically prefixed with / so the ^ in the regexp not being followed by / will probably cause it not to match. Just something you may need to experiment with.

1 Like

I changed it!

# gnuboard rewrite rules
@content path_regexp content ^/content/([0-9a-zA-Z_]+)$
rewrite @content /bbs/content.php?co_id={re.content.1}&rewrite=1

@title path_regexp title ^/content/([^/]+)/$
rewrite @title /bbs/content.php?co_seo_title={re.title.1}&rewrite=1

@rss path_regexp rss ^rss/([0-9a-zA-Z_]+)$
rewrite @rss /bbs/rss.php?bo_table={re.rss.1}

@board path_regexp board ^/([0-9a-zA-Z_]+)$
rewrite @board /bbs/board.php?bo_table={re.board.1}&rewrite=1

@write path_regexp write ^/([0-9a-zA-Z_]+)/write$
rewrite @write /bbs/write.php?bo_table={re.write.1}&rewrite=1

@seo path_regexp seo ^/([0-9a-zA-Z_]+)/([^/]+)/$
rewrite @seo /bbs/board.php?bo_table={re.seo.1}&wr_seo_title={re.seo.2}&rewrite=1

@id path_regexp id ^/([0-9a-zA-Z_]+)/([0-9]+)$
rewrite @id /bbs/board.php?bo_table={re.id.1}&wr_id={re.id.2}&rewrite=1

Is it alright?

2 Likes

It works really well!!

thx so much!

1 Like

Awesome, glad you figured it out. If your config works for other gnuboard users too, then consider posting a guide in our wiki!

1 Like

Unfortunately the first line doesn’t seem to work.
I’m looking at the log, but it’s harder because I don’t see any bugs.
Caddy is rewriting to the exact short address, but it is not linked to the content.

I think it’s cause of this:

Add a / between ^ and content, I think.

You are right!

It’s perfect!

1 Like

I agree with you.

Last question.

This is a url to edit posts that have already been made.

How can I rewrite url

from

https://example.com/free/write?w=u&wr_id=28&page=

to

https://example.com/bbs/write.php?w=u&bo_table=free&wr_id=28&page=

@write path_regexp write ^/([0-9a-zA-Z_]+)/write$
rewrite @write /bbs/write.php?bo_table={re.write.1}&rewrite=1

As you can see this code does not work.

Now I’ve entered the final chapter of gnuboard, but it’s too hard.

Plz help me.


It’s done.

Final Code

Caddy 2 for gnuboard rewrite rule

@content path_regexp content ^/content/([0-9a-zA-Z_]+)$
rewrite @content /bbs/content.php?co_id={re.content.1}&rewrite=1
@title path_regexp title ^/content/([^/]+)/$
rewrite @title /bbs/content.php?co_seo_title={re.title.1}&rewrite=1
@rss path_regexp rss ^rss/([0-9a-zA-Z_]+)$
rewrite @rss /bbs/rss.php?bo_table={re.rss.1}
@board path_regexp board ^/([0-9a-zA-Z_]+)$
rewrite @board /bbs/board.php?bo_table={re.board.1}&rewrite=1
@write path_regexp write ^/([0-9a-zA-Z_]+)/write$
rewrite @write /bbs/write.php?bo_table={re.write.1}&{query}
@seo path_regexp seo ^/([0-9a-zA-Z_]+)/([^/]+)/$
rewrite @seo /bbs/board.php?bo_table={re.seo.1}&wr_seo_title={re.seo.2}&rewrite=1
@id path_regexp id ^/([0-9a-zA-Z_]+)/([0-9]+)$
rewrite @id /bbs/board.php?bo_table={re.id.1}&wr_id={re.id.2}&rewrite=1

These are perfect codes for gnuboard 5.4.

2 Likes

You can actually use {query} which is the Caddyfile shortcut for that placeholder:

Great detective work! Happy to see you figured it all out

:grin:

Thank you for your information. I will change it.

@content path_regexp content ^/content/([0-9a-zA-Z_]+)$
rewrite @content /bbs/content.php?co_id={re.content.1}&{query}
@title path_regexp title ^/content/([^/]+)/$
rewrite @title /bbs/content.php?co_seo_title={re.title.1}&{query}
@rss path_regexp rss ^rss/([0-9a-zA-Z_]+)$
rewrite @rss /bbs/rss.php?bo_table={re.rss.1}
@board path_regexp board ^/([0-9a-zA-Z_]+)$
rewrite @board /bbs/board.php?bo_table={re.board.1}&{query}
@write path_regexp write ^/([0-9a-zA-Z_]+)/write$
rewrite @write /bbs/write.php?bo_table={re.write.1}&{query}
@seo path_regexp seo ^/([0-9a-zA-Z_]+)/([^/]+)/$
rewrite @seo /bbs/board.php?bo_table={re.seo.1}&wr_seo_title={re.seo.2}&{query}
@id path_regexp id ^/([0-9a-zA-Z_]+)/([0-9]+)$
rewrite @id /bbs/board.php?bo_table={re.id.1}&wr_id={re.id.2}&{query}

change from rewrite=1 to {query}

1 Like

This topic was automatically closed after 30 days. New replies are no longer allowed.