Home > Back-end >  I'm editing an .htaccess file with regex redirects, how to add "-" to \w pattern?
I'm editing an .htaccess file with regex redirects, how to add "-" to \w pattern?

Time:11-03

I have the following redirect in .htaccess file:

RewriteRule ^tasks/(\w )/?$ /projects.php?task=$1

If I'm trying to access smth like /tasks/nov-1 it will say object not found as \w does not accept "-". Can you please help with adding "-" in the above example redirect rule?

CodePudding user response:

You may use this rule to accept hyphen and also it is recommended to use correct flags in this rule:

RewriteRule ^tasks/([\w-] )/?$ projects.php?task=$1 [L,QSA,NC]

Flags are:

  • L is for Last rule
  • QSA is for query string append
  • NC is for ignore case

Please note that L flag marks the last rule for current iteration of mod_rewrite loop. It behaves like continue in a while loop not like a break so it doesn't end the loop immediately. The rewriting process will be started again if the request has been rewritten. So other rules may get applied after the rule with the L flag.

On Apache 2.4 there is an END flag which acts like break and immediately halts the full rewriting process.

  • Related