How can I hide a query string from the url?

3

I want to hide the query strings of the url, so I know if the url has the query and only get the value of the query.

Let's say this is my url:

http://miurl.com/?query=valor

And I want it to be modified in this way:

http://miurl.com/valor

But that does not allow in any way the url has the query as in the first example. What I mean is to create friendly urls, and if a query is detected in the url it will automatically redirect it to the url with only the value of the query.

Let's say we write the url like this:

http://miurl.com/?query=valor

And redirect to:

http://miurl.com/valor

Now the question is, how can I achieve it with htaccess?

My htaccess file:

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ /?query=$1 [L,NC]
    
asked by Eddy Otsutsuki 09.03.2017 в 23:51
source

1 answer

2

This is what you need

DirectoryIndex index.html
RewriteEngine on
RewriteBase /

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*) /index.html?query=$1 [R=301,L]
  • On the first line RewriteEngine On active redirects.
  • I omit the following two lines because you do not need them for your problem, but you should consider them if you have other uses.
  • The last line makes the redirection RewriteRule ^(.*) /?query=$1 [R=301,L]
    • First it indicates that it is a rewrite rule RewriteRule
    • Next, ^(.) captures (or makes match ) all the characters from the start
    • This part /?query=$1 is what is sent to the server. The captured result is attached to the string /?query=
    • The R=301 flag indicates that the page was moved from permanent form
    • The L flag indicates that it is the last rule and that > others should not be processed, if any.
  

In your case, only replace the last line.

    
answered by 10.03.2017 / 04:09
source