0

Current htaccess block :

# redirect "/search/location.php?state=x&city=x&name=x" to "/search/state/city/name/"
RewriteCond %{THE_REQUEST} \s/search/location.php\?state=(.+)&city=(.+)&name=(.+)\s [NC]
RewriteRule ^ /search/%1/%2/%3/? [R=301,L]
#### internally rewrite "/search/state/city/name/" to "/search/location.php?state=x&city=x&name=x"
RewriteRule ^search/(.+)/(.+)/(.+)/$ /search/location.php?state=$1&city=$2&name=$3 [L]

https://example.com/search/location.php?state=florida&city=Orlando&name=test

correctly redirects to https://example.com/search/florida/orlando/test/

https://example.com/search/location.php?state=florida&city=Orlando&name=test&something=something

incorrectly redirects to https://example.com/search/florida/orlando/test&something=something/

What I want to achieve is redirecting to https://example.com/search/florida/orlando/test/?something=something

What am I missing here? Keep in mind I am doing an internal as well.

3
  • Your desired URL https://example.com/search/florida/orlando/test/&something=something does not really make sense. There is no query string in there, but an ampersand notation. Commented Oct 11 at 9:15
  • About the actual issue: you explicitly implemented everything after name= to be accepted as parameter value for name. So that is what happens. I would suggest to change the matching pattern to something like that: \s/search/location.php\?state=([^&]+)&city=([^&]+)&name=([^&]+)(.*)\s, whoch captures anything following the name parameter value as a fourth capture group which you then can use however you like. Commented Oct 11 at 9:18
  • That should have been https://example.com/search/florida/orlando/test/?something=something as desired url. Sorry for the mistake. Commented Oct 11 at 10:59

1 Answer 1

0

I was able to get this working how I wished for anyone interested. It handles any 'extra' parameters both ways.

# redirect "/search/location.php?state=x&city=x&name=x" to "/search/state/city/name/"
RewriteCond %{THE_REQUEST} \s/search/location\.php\?state=([^&]+)&city=([^&]+)&name=([^&]+)(&(.*))?\s [NC]
RewriteRule ^ /search/%1/%2/%3/?%5 [R=301,L]
#### internally rewrite "/search/state/city/name/" to "/search/location.php?state=x&city=x&name=x"
RewriteRule ^search/([^/]+)/([^/]+)/([^/]+)/?$ search/location.php?state=$1&city=$2&name=$3 [L,QSA]
Sign up to request clarification or add additional context in comments.

Comments

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.