Help with a rewrite regex

But that’s exactly what I need ! And it’s not working, even if I put this rewrite above the other one… May I have to use the if match logic ?

WordPress doesn’t need to be involved, I just want the URL to be rewritten by removing some information in it. It’s purely static and it would be the same if I used Hugo instead of WordPress. And if I moved to a static engine, how could I redirect old URLs to the new one if Caddy does not let me do this simple rewrite ?

I guess I understand why you say that @matt, but I think it’s better if Caddy handles this kind of thing. I would rely on Apache or NGINX, so why not Caddy ?

Wait so in WordPress you have set the url to your old posts to not contain the date?

ie. You’ve changed the url and you just need to 301 the old links to the new one?

Indeed. The issue is with old links, inside existing posts (those, I could find and correct myself) but also anywhere on the internet. These old links should be automatically redirected to the new links. Using a 301 redirect, yes.

For years, I displayed dates in my URLs (which is the WordPress default), and then, I switched to simpler permalinks, with only the slug of the post after the domain name. Everything works then inside WordPress (for instance, all archive pages are updated accordingly), but you have to use a rewrite rule on your server to redirect old links.

It looks like this :

Does WP have any way to redirect links to dated posts?

No, not to my knowledge at least.

EDIT : well, some plugins do this, but I don’t think it’s a good idea. I hope to move to Hugo one day, and I wish to keep this redirect for old URLs…

I think you will need a new plugin for this.

Something like

permalinks {
    path /path/to/map
}

And in the file you would have

example.com/old/style/links,example.com/new-style-links
example.com/some/other/link,example.com/some-other-link
2 Likes

Maybe but I don’t understand why the regex is not working. Because if it was working, what Caddy offers right now is enough, I think.

But the thing is you don’t want rewrite, you want to redirect old style links to the new style.

I’m not sure if the slashes need to be escaped. Have you tried without escaping?

Yes, nothing. I have the feeling the rewrite command is not taken into account at all by Caddy…

Well, maybe, but redirects in Caddy do not work with regex, correct ?

Correct, that’s why I think we need a separate permalinks plugin so redir doesn’t become too complicated.

1 Like

Well, I don’t know enough about Caddy to know for sure, but it seemed to me a rewrite rule would be enough… if it worked, that is.

If rewrite worked then everyone would still use the old style links for older posts. The of rewrite as a proxy to a different path. It would still show to old link in people’s browser.

Oh OK, I didn’t get that.

Maybe the plugin is the way to go then. But I think Caddy needs a system for this kind of redirects.

It’s a great fit for a plugin. Matt doesn’t want Caddy doing too many things in the core. But don’t worry, plugins are super easy to get! I’ll look into how to make one for 0.9. So it may be a little while before I come up with anything since .9 isn’t out yet.

2 Likes

I think @abiosoft was looking into making redir support some basic conditionals, maybe even regexp…

1 Like

You don’t seem to excited about the idea. :wink:

what’s the status on this now with 0.9 ? @jacob a plugin for this available yet ?

Yes, redir now supports regex using the if match condition. e.g. if {path} match /regex. But it does not support numbered regex captures like rewrite does.

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.