wordpress-website-lifecycle/mu-plugins/_core-auto-trailing-slash.php
Viktor Szépe d38d9e7c8e
Some checks failed
Back-end / Syntax errors (push) Failing after 2s
Spelling / 文A Typos check (push) Failing after 2m28s
Integrity / Integrity (push) Has been cancelled
Implement trailing slash for no-dot paths (#61)
* Implement trailing slash for no-dot paths

Adds a filter to mod_rewrite_rules to enforce trailing slashes for URLs without dots.

* Clarify conditions for trailing slash in comment

Update comment to clarify conditions for adding trailing slash.

* Update _core-auto-trailing-slash.php
2026-07-16 05:05:57 +02:00

25 lines
634 B
PHP

<?php
/*
* Plugin Name: Trailing slash for no-dot paths
* Plugin URI: https://github.com/szepeviktor/wordpress-website-lifecycle
*/
add_filter(
'mod_rewrite_rules',
static function ($rules) {
$insertion = <<<HTACCESS
# Add trailing slash if URL doesn't contain a dot and doesn't already end with /
RewriteCond %{REQUEST_URI} !(\.|/$)
RewriteRule ^(.+)$ /$1/ [R=301,L]
HTACCESS;
$needle = "RewriteEngine On\n";
if (strpos($rules, $needle) === false) {
return $needle.$insertion.$rules;
}
return str_replace($needle, $needle.$insertion, $rules);
},
20,
1
);