1

I have what I thought was a very straightforward question, but I can't seem to find the answer.

I have a page that reads in a zip like so: http://blahblah.com/searchbyzip/?90036

The index.php file there then reads in the zip as a query and using PHP code, the value is manipulated...

I want the user to enter the same URL, but without the question mark. So, they should type and see the following instead: http://blahblah.com/searchbyzip/90036

I know there is a way to do as a rewrite in .htaccess file. However, I am not very proficient in Apache rewrite. How would I go about doing this?

1 Answer 1

1

You can do it like this in your .htaccess:

RewriteEngine on
RewriteRule ^searchbyzip/(\d+)$ searchbyzip/?$1

If you need any query strings to be passed through then add [QSA] at the end of the rule, preceded by a space. It considers a valid "zip" to be any number of digits. If this should be changed in some way let me know.

A version to accept a-z (case-insensitive), numbers, underscores or dashes would be:

RewriteEngine on
RewriteRule ^searchbyzip/([a-zA-Z0-9_-]+)$ searchbyzip/?$1
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. It worked. I had something similar, but not quite that... What if I wanted it to accept non-numerics?
I added a version to demonstrate an option for that. You could add to the character class but remember to keep the hyphen at the end or escape it.

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.