I’m the worst when I have to deal with regex, my brain just exploses every time I have to do some…
I have a regex rule for my blog, to translate URLs from https://voiretmanger.fr/2014/03/12/will-hunting-van-sant/
to https://voiretmanger.fr/will-hunting-van-sant/
, so I want to convert the URL by removing the date in the middle.
In the actual .htaccess
, here’s what I have :
RedirectMatch 301 ^/([0-9]{4})/([0-9]{2})/([0-9]{2})/(.*)$ https://voiretmanger.fr/$4
Here’s what I have right now, but it does not work. I have a 404 instead of the redirect when I try a URL with the date in it.
rewrite {
r ^/([0-9]{4})/([0-9]{2})/([0-9]{2})/(.*)
to /{2}
}
Thanks if you can help me !
jacob
(Jacob Hands)
June 7, 2016, 5:22pm
2
Try
rewrite {
r ^\/([0-9]{4})\/([0-9]{2})\/([0-9]{2})\/(.*)
to /{4}
}
Thanks, but it does not work…
Maybe I should have added that it’s a WordPress blog, but I don’t know if it makes a difference.
jacob
(Jacob Hands)
June 7, 2016, 5:31pm
4
Doesn’t WordPress have an option to remove the date?
For all new URLs, yes, but you have to use this regex for the old URLs, external or internal. Otherwise, you have 404 responses…
jacob
(Jacob Hands)
June 7, 2016, 6:00pm
6
See my original reply. I think I fixed it (you need to escape / )
hacdias
(Henrique Dias)
June 7, 2016, 6:05pm
7
If it doesn’t work, try this:
rewrite {
r ^\/([0-9]{4})\/([0-9]{2})\/([0-9]{2})\/(.*)
to /{4}
}
I changed to 4 because it’s the fourth group you want ((.*)
).
Hope it helps!
jacob
(Jacob Hands)
June 7, 2016, 6:16pm
8
Thanks! I thought it was the 4th group but was having second thoughts. I’ll change mine.
Well, I have tried with a 4, so it’s not the solution, I’m afraid…
jacob
(Jacob Hands)
June 7, 2016, 6:19pm
10
I also fixed your regex in my edit.
Please try this:
rewrite {
r ^\/([0-9]{4})\/([0-9]{2})\/([0-9]{2})\/(.*)
to /{4}
}
Edit: this shows the regex is working
The regex might be working, but in the end… 404.
Maybe there’s something wrong with another rewrite ? I used the one in GitHub for WordPress, so :
rewrite {
if {path} not_match ^\/wp-admin
to {path} {path}/ /index.php?_url={uri}
}
Maybe there is a conflict ?
jacob
(Jacob Hands)
June 7, 2016, 6:31pm
12
Can you post your entire Caddyfile?
hacdias
(Henrique Dias)
June 7, 2016, 6:42pm
13
One thing: try removing the “^”. I don’t know if it will change anything, but it’s worth trying.
Here it is :
https://www.voiretmanger.fr {
redir https://voiretmanger.fr{uri}
}
https://voiretmanger.fr {
root /srv/users/serverpilot/apps/voiretmanger/public
log /srv/users/serverpilot/log/voiretmanger/access.log {
rotate {
size 100 # Rotate after 100 MB
age 14 # Keep log files for 14 days
keep 10 # Keep at most 10 log files
}
}
errors /srv/users/serverpilot/log/voiretmanger/error.log
# PHP-FPM with Unix socket
fastcgi / /srv/users/serverpilot/run/voiretmanger.php-fpm.sock php
gzip
# Routing for WordPress
rewrite {
if {path} not_match ^\/wp-admin
to {path} {path}/ /index.php?_url={uri}
}
rewrite {
r ^\/([0-9]{4})\/([0-9]{2})\/([0-9]{2})\/(.*)
to /{4}
}
# Redirect personnels
redir /a-propos/publicite /soutien
redir /archives/carte-des-restaurants /a-manger
}
@hacdias : nope…
jacob
(Jacob Hands)
June 7, 2016, 6:59pm
15
What is this supposed to be doing? to {path} {path}/ /index.php?_url={uri}
Hum, I have no idea to be frank. I took this command here : examples/Caddyfile at master · caddyserver/examples · GitHub
jacob
(Jacob Hands)
June 7, 2016, 8:08pm
17
What do your wordpress URL’s look like without any rewriting?
jacob
(Jacob Hands)
June 7, 2016, 8:32pm
19
Try removing this then:
rewrite {
if {path} not_match ^\/wp-admin
to {path} {path}/ /index.php?_url={uri}
}
Without it, the site does not work : I have Caddy 404 if I try to open a page. So even if I don’t understand why, these lines are important for WordPress.