htaccess modrewrite problem with rewriting url with- dashes solution

I was getting frustrated with trying to write a simple URL like this:

/example-withdash

I used the htaccess code like this:

Rewriterule ^example-withdash$ / [R=301]

I also tried escaping the dash which I thought should have treated it as a literal but that didn't work either:

Rewriterule ^example\-withdash$ / [R=301]

But it wouldn't work, apparently the "-" dash means don't substitute, so this means in plain Engilsh that my rule is not treated as intended or expected.

- (dash)
A dash indicates that no substitution should be performed (the existing path is passed through untouched). This is used when a flag (see below) needs to be applied without changing the path.
  http://httpd.apache.org/docs/current/mod/mod_rewrite.html

The Solution:

Put brackets around the rewrite rule ()

Rewriterule ^(example-withdash)$ / [R=301]

Now it works, it's very simple but quite frustrating when you don't know this.  I actually think Apache should have a debug mode or some switch to run an .htaccess file and verbosely output the interpretation of each rule.


Tags:

htaccess, modrewrite, rewriting, url, dashes, solutioni, frustrated, withdash, rewriterule, escaping, dash, literal, didn, wouldn, quot, substitute, engilsh, indicates, substitution, performed, existing, untouched, applied, http, httpd, apache, org, docs, mod, mod_rewrite, html, brackets, rewrite, frustrating, debug, mode, verbosely, output, interpretation,

Latest Articles

  • Linux Ubuntu Cannot Print Large Images
  • Cannot Print PDF Solution and Howto Resize
  • Linux Console Login Screen TTY Change Message
  • Apache Cannot Start Listening Already on 0.0.0.0
  • MySQL Bash Query to pipe input directly without using heredoc trick
  • CentOS 6 and 7 / RHEL Persistent DHCP Solution
  • Debian Ubuntu Mint rc-local service startup error solution rc-local.service: Failed at step EXEC spawning /etc/rc.local: Exec format error
  • MySQL Cheatsheet Guide and Tutorial
  • bash script kill whois or other command that is running for too long
  • Linux tftp listens on all interfaces and IPs by DEFAULT Security Risk Hole Solution
  • python import docx error
  • Cisco Unified Communications Manager Express Cheatsheet CUCME CME
  • Linux Ubuntu Debian Missing privilege separation directory: /var/run/sshd
  • bash how to count the number of columns or words in a line
  • bash if statement how to test program output without assigning to variable
  • RTNETLINK answers: Network is unreachable
  • Centos 7 how to save iptables rules like Centos 6
  • nfs tuning maximum amount of connections
  • qemu-kvm error "Could not initialize SDL(No available video device) - exiting"
  • Centos 7 tftpd will not work with selinux enabled