Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic .htaccess rules

I updated my site twice since the last year and I have different page names since my first version. Is it possible to create dynamic rules in apache?

for example:

I use to have these pages (example):

  • /abc123 (version 1)
  • /abc_123 (version 2)
  • /abc-123/ (version 3 - current)

I want to create a rule that will redirect abc123 to abc_123 if it exists and abc_123 to abc-123 if it exists. Thanks

like image 812
Erin Tucker Avatar asked Oct 25 '11 03:10

Erin Tucker


1 Answers

Why not this?

RewriteRule ^([a-z]+)([0-9]+)$ /$1-$2 [NC,L]
RewriteRule ^([a-z]+)_([0-9]+)$ /$1-$2 [NC,L]

EDIT:

or you can use 1 rule:

RewriteRule ^([a-z]+)(_?)([0-9]+)/?$ /$1-$3 [NC,L]

this way it's easy to add characters in the (_?) instead of creating the same rule over and over with a little variance.

like image 186
James M. Avatar answered Sep 21 '22 10:09

James M.