Erin Tucker
Erin Tucker

Reputation: 3324

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):

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

Upvotes: 10

Views: 522

Answers (2)

James M.
James M.

Reputation: 352

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.

Upvotes: 16

Zan Lynx
Zan Lynx

Reputation: 54325

I found this example:

RewriteEngine on
#
# If the requested URL exists as a file with a .php extension
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
# internally rewrite requested .html URL to .php file
RewriteRule ^(([^/]+/)*[^.]+)\.html$ /$1.php [NC,L]

You will want to read about the various options and edit it to make it work for your situation.

Upvotes: -6

Related Questions