Mamedul Islam
Mamedul Islam

Reputation: 71

.htaccess rewrite for multi-query string with only one query as path

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
)

Upvotes: 0

Views: 207

Answers (1)

hjpotter92
hjpotter92

Reputation: 80647

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

Upvotes: 1

Related Questions