'Remove query string (?page=login) using .htaccess

I'm trying to remove query string but it's not working this code I generated from https://www.301-redirect.online/htaccess-rewrite-generator and I really can't find it wrong anywhere but it's not working please help me

RewriteCond %{QUERY_STRING} ^page\=login$
RewriteRule ^$ /login? [L]

My .htaccess file

RewriteEngine On

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]


RewriteCond %{QUERY_STRING} ^page\=login$
RewriteRule ^$ /login? [L]

php_flag display_errors 1


Solution 1:[1]

RewriteRule ^$ /login? [L]

This is an internal rewrite, not a redirect. To issue an external redirect and actually remove the query string from the request then you need the R (redirect) flag.

For example:

RewriteRule ^$ /login? [R=302,L]

However, this does not simply remove the query string, it also redirects to the URL-path /login. (And there's nothing in your .htaccess file that would allow this to work.) To simply remove the query string, as stated in your question then you need to remove login from the substitution string. For example:

RewriteRule ^$ /? [R=302,L]

If this is intended to be permanent then change to a 301, but only once you have confirmed that it works as intended.


UPDATE:

I want change ???.???/?page=login to ???.???/login

Ok, but that is quite different to what your question is asking.

Make sure you are already linking to /login in your application then add the following to your .htaccess file, replacing the existing rule:

# Redirect old requests for the query string
RewriteCond %{THE_REQUEST} ^[A-Z]{3,7}\s/\?page=login\s
RewriteRule ^$ /login [QSD,R=301,L]

# Rewrite requests for "/login" to actual URL that handles the request
RewriteRule ^login$ index.php?page=login [L]

Assuming index.php is the actual end-point (file) that handles the request.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1