Hi

I have changed some urls and know how to do a normal redirect but do not know where to start to be abale to redirect if part of the url contains certain words Example

thisdomain.com/this/now-when/ would be redireted to thisdomain.com/this/an-other/

There is quite a few of them with the ending so cant just type each one in as a normal redirect.

Hope someone can help

Thanks

Dani AI

Generated

— two practical ways to do this depending on how many mappings you need and where they live. — if slugs are stored in a DB, the dynamic options below will be relevant.

For a simple site-wide rule (fast, no PHP), use mod_alias’s RedirectMatch. This regex runs against the full path (leading slash included) and will turn any URL that ends with now-when into the same prefix with an-other:

RedirectMatch 301 ^/(.*)now-when/?$ /$1an-other/

This handles /this/now-when/ => /this/an-other/ and /now-when/ => /an-other/. If you need to require a slash before the slug, change the pattern to ^/(.*)/now-when/?$.

If you need more control (loop protection, case-insensitive, query-string handling) use mod_rewrite in an .htaccess placed at the site root. Use a 302 while testing and switch to 301 when confirmed:

RewriteEngine On
RewriteCond %{REQUEST_URI} !an-other [NC]
RewriteRule ^(.*)now-when/?$ /$1an-other/ [R=302,L,NC,QSA]

Notes and troubleshooting:

  • 301s are cached by browsers. Test with curl -I or use 302 then switch to 301.
  • Put these rules before CMS/WordPress rules so they run first.
  • Ensure mod_rewrite is enabled and AllowOverride All if using .htaccess.
  • If you have many different pairings, use RewriteMap in the server config or a small PHP lookup that reads your DB and issues Location headers (RewriteMap and server-wide config are not allowed in .htaccess).
  • Avoid redirect loops by excluding the target (see RewriteCond) and be explicit about trailing slashes if consistency matters.

Hi,

can you explain it a bit more? If I've understood you want to redirect only certain urls basing on regular expressions, correct? Are these slugs (now-when, an-other) saved into a database?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.