htaccess condition and rule.
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

I need a htaccess rule

condition exclude website.com/?q=

condition exclude website.com/any/ (slash at the end)

rewrite website.com/any to website.com/?p=any

rewrite website.com/any-any to website.com/?p=any+any

rewrite website.com/any-any-any to website.com/?p=any+any+any

rewrite website.com/any-any-any-any to website.com/?p=any+any+any+any

awarded to Titus

Crowdsource coding tasks.

2 Solutions


Tell me if this works

RewriteEngine On
RewriteRule ^(\?p=)?demo([^-]*)\-(.*) ?p=demo$2\ $3 [N]
test here, https://htaccess.mwl.be/ check updated bounty.
feligosky 3 months ago
Winning solution

Try this:

RewriteEngine On
RewriteBase /
RewriteRule ^([^\/-]+)$ ?p=$1 [R=301,L]
RewriteRule ^([^\/-]+)\-([^\/-]+)$ ?p=$1+$2 [R=301,L]
RewriteRule ^([^\/-]+)\-([^\/-]+)\-([^\/-]+)$ ?p=$1+$2+$3 [R=301,L]
RewriteRule ^([^\/-]+)\-([^\/-]+)\-([^\/-]+)\-([^\/-]+)$ ?p=$1+$2+$3+$4 [R=301,L]

You can take a look at how this work here: https://htaccess.mwl.be?share=2519d802-4550-59a0-b165-0bfb29199529

thanks for your reply, please add exclude condition if url have slash at the end.
feligosky 3 months ago
I've forgot to account for the ?q= cases. I've added a new rule to deal with that
Titus 3 months ago
http://example.com/any-any (working fine. ) but if you test but http://example.com/any-any/ slash and the end must be exclude from rewrite.
feligosky 3 months ago
You're right. I've used forward slashes instead of backwards slashes in the regex. I've updated the solution. I think this time it will work as expected.
Titus 3 months ago
Thanks, excelent!
feligosky 3 months ago
I'm glad I could help. Good luck.
Titus 3 months ago
View Timeline