简体   繁体   中英

.htaccess permanent redirect with query string

I want to have a permanent redirect where whoever visits the old url is automatically sent to the new one:

From: https://example.io/users?username=value To: https://example.io/value

The new URL already works, but I just need the redirect. I already have this code:

RewriteBase /
RewriteCond  %{REQUEST_FILENAME} !-f
RewriteCond  %{REQUEST_FILENAME} !-d
RewriteRule  ^(.*)$ users.php?username=$1 [QSA,L]

I'd say this should roughly be what your question asks for:

RewriteEngine on
RewriteCond %{QUERY_STRING} ^username=(.+)$
RewriteRule ^/?users\.php/?$ /%1 [R=301]

Those lines should work likewise in the http servers host configuration or in dynamic configuration files (".htaccess").

It redirects all incoming requests to the old URL " /users.php?username=value . I fail to see how the code you posted should help here, which is why I wrote above rules from scratch instead of modifying yours.

And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM