2

I have a ReWrite rule in my .htaccess file right now:

RewriteRule ^item/?([a-zA-Z0-9_]+)?/?([a-zA-Z0-9_]+)?/?$ static/items.php?a=$1&b=$2 [NC,L]

It will pick any items that have an underscore:

item/new_item/new_order

However, I need to change from underscore to dashes to make it:

item/new-item/new-order

If I simply make a change in RewriteRule string it breaks it. Not sure how to fix that.

RewriteRule ^item/?([a-zA-Z0-9-]+)?/?([a-zA-Z0-9-]+)?/?$ static/items.php?a=$1&b=$2 [NC,L]
anubhava
  • 761,203
  • 64
  • 569
  • 643
santa
  • 12,234
  • 49
  • 155
  • 255
  • I am assuming you don't want a rule that goes from `item/new_item/new_order` to `item/new-item/new-order`, but a rule that matches the latter one? The wording in your question is confusing me. – Sumurai8 Oct 20 '13 at 16:57
  • Yes, I want to change it to "-". Currently it is "_". – santa Oct 20 '13 at 17:03

1 Answers1

0

This is fairly tricky stuff but can be done using following Rewrite rules:

RewriteEngine On

# first replace each _ by - recursively
RewriteRule ^(item)/([^_]*)_(.*)$ /$1/$2-$3 [L,NC]

# now usual stuff to forward request to static/item.php
RewriteRule ^(item)/([^_/]*)/?([^_/]*)/?$ static/$1.php?a=$2&b=$3 [L,QSA,NC]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • This looks interesting, but won't this change my previous approach where the $1 and $2 were optional? All I need to change in my current string is the wat the words are connected, from underscore to dash. – santa Oct 20 '13 at 17:08
  • Do you want to make both `$1` and `$2` optional? – anubhava Oct 20 '13 at 17:13
  • Made some minor edit, it should work by making both `$1 and $2 optional` – anubhava Oct 20 '13 at 17:15