tanghus
tanghus

Reputation: 55

Remove "?" from the query string

I have decided to ditch Wordpress and move to Pico CMS instead.

Now I'm struggling with getting the URL rewriting done correctly.

I have managed to rewrite the query string from eg. https://www.example.org?page to https://www.example.org/page using RewriteRule ^(.*) ?$1 [L], but now I also want it rewritten if there is a query string.

Example:

If https://www.example.org?page is requested it loads just fine, but I want to have it rewritten as https://www.example.org/page - at least in the location bar.

I have tried several variations but to no avail. I suck at regular expressions...

RewriteCond %{QUERY_STRING} !^$ RewriteRule ^\\?/(.*)$ $1

Nope

RewriteRule (^\?$) https://tanghus.net/$1 [NC,R=301,L] RewriteRule ^.*$ https://tanghus.net/? [NC,R=301,L]

no dice

RewriteCond %{QUERY_STRING} . RewriteRule ^$ %{QUERY_STRING} [R,L]

Pfff

RewriteCond %{THE_REQUEST} \?\sHTTP [NC] RewriteRule ^ %{REQUEST_URI} [L,R]

Giving up :(

Upvotes: 0

Views: 78

Answers (1)

Amit Verma
Amit Verma

Reputation: 41249

You can use this Rule

RewriteEngine on

# externally  redirect from /?page to /page
RewriteCond %{THE_REQUEST} /\?([^\s]+) [NC]
RewriteRule ^/?$ /%1? [L,R]
# internally map /page to /?page
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?(.+)$ /?$1 [L]

Upvotes: 2

Related Questions