0

I tried .htaccess rewrite from

https://example.com/?page=contact&query1=test1&query2=test2

to

https://example.com/contact/?query1=test1&query2=test2

with output as

Array
(
    [page] => contact
    [query1] => test1
    [query2] => test2
)

I tried the rewrite below but failed to get output-

RewriteEngine On
RewriteRule ^(.*?)//?(.*)$ /?page=$1&$2 [NC,L]

Here I should include that I can make any query after https://example.com/contact/ will make output as follow as similar to regular get string with "page" query.

Such as-

https://example.com/?page=contact2&query15=test14&query25=test24&query35=test34

will output the $_GET query string

Array
(
    [page] => contact2
    [query15] => test14
    [query25] => test24
    [query35] => test34
)

1 Answer 1

1

The following rules should do the trick:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^/?([^/]+).* /?page=$1&%1 [NC,L]

We check if the link being opened is not a valid/existing file or directory, then we capture the current query string and lastly we rewrite the visited page.

See it in action at: https://htaccess.madewithlove.be?share=1e473616-5364-5718-8819-a8032a5319c1

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.