1

I have the following rule to rewrite URLs which works for single word parameters. However, it doesn't work if there is a "-" in the variable. How can I modify this to accomplish this?

Old URL: domain.com/medications/?drug=medication-title

New URL: domain.com/medications/medication-title

RewriteRule ^medications/([^/d]+)/?$ medications/?drug=$1 [QSA]

Like I said, it works when there is no "-" in the name, but need it to handle all passed parameters.

Thanks for your help.

1

1 Answer 1

1

You need to fix your regex by using this rule in root .htaccess:

Options -MultiViews
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^medications/([^/]+)/?$ medications/?drug=$1 [QSA,L,NC]
Sign up to request clarification or add additional context in comments.

3 Comments

Unfortunately, this gives me an internal server error. If I add the "d" back into the ([^/d]+) aspects, it will process the single word parameters, but not the ones with the "-".
After doing some testing, I found a different issue that I missed before. Some of the medication names have a "/" in the title. So, with the rewrite it now treats it as a new subfolder. Is there a way to escape those, change them to a "-" or something along those lines?
Hmm that should be covered in a new question as I think that rule will be a bit complex and very different from this one. Once you open a new question I will try to address it there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.