How do I include hyphens in a htaccess regex?

  • RedBMedia
  • Proficient
  • Proficient
  • User avatar
  • Posts: 315

Post 3+ Months Ago

I am currently rewriting URLs with a htaccess file to create cleaner URLs. Here is my htaccess file:

Code: [ Select ]
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([a-zA-Z0-9]+)/$ /page.php?s=$1
RewriteRule ^([a-zA-Z0-9]+)$ /page.php?s=$1
  1. Options +FollowSymlinks
  2. RewriteEngine on
  3. RewriteRule ^([a-zA-Z0-9]+)/$ /page.php?s=$1
  4. RewriteRule ^([a-zA-Z0-9]+)$ /page.php?s=$1


With this regex I am able to have URLs like:

Code: [ Select ]
http://mysite.com/foobar


But I also want URLs like:

Code: [ Select ]
http://mysite.com/foo-bar


How do I edit the regex to include a hyphen when needed?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

For your pattern just add a hyphen,

Code: [ Select ]
[a-zA-Z0-9-]
  • RedBMedia
  • Proficient
  • Proficient
  • User avatar
  • Posts: 315

Post 3+ Months Ago

Thanks a ton Spoof! For your hospitality. Samuele L Jackson is pleased.


Image

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: ScottG and 82 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.