I want to add "new" to every blog URL slug.
For example, if someone visits https://www.example.com/blog/post-one
it will redirect to https://www.example.com/blog/new-post-one
.
CodePudding user response:
At the top of the root .htaccess
file try the following using mod_rewrite:
RewriteEngine On
# Redirect "/blog/<post-title>" to "/blog/new-<post-title>"
RewriteRule ^(blog)/(?!new-)([\w-] )$ /$1/new-$2 [R=302,L]
This assumes <post-title>
consists of only the characters a-z
, A-Z
, 0-9
, _
(underscore) and -
(hyphen)
(?!new-)
- this is a negative lookahead that asserts that the <post-title>
does not already start with new-
. Otherwise we would get an endless redirect loop.
You do not need to repeat the RewriteEngine
directive if it already occurs (later) in the file.