htaccess Rewrite rule to send variable info to script but display clean url

+1 vote
asked Sep 28, 2015 by ronald-fernandes

What I'd like to do is when a URL like

http://localhost/sandbox/jcsearch/country/countryname

is typed by the user I would like to to point to

http://localhost/sandbox/jcsearch/index.php?country=countryname

but still retain the original clean URL in the address bar ie

http://localhost/sandbox/jcsearch/country/countryname

Is this possible? would it create any kind of redirect loop?

1 Answer

+1 vote
answered Nov 29, 2018 by hjpotter92

The rewrite would happen as follows:

RewriteEngine on
RewriteRule ^sandbox/jcsearch/([^/]+)/([^/]+)$ sandbox/jcsearch/index.php?$1=$2 [L,NC]

Since, the RewriteRule directive does not have the redirection flag (R) set, it will not change the URL in your browser's addressbar. So, by visiting

http://localhost/sandbox/jcsearch/country/countryname

user will get internally redirected to:

http://localhost/sandbox/jcsearch/index.php?country=countryname

Please note: You have to put the rewrite rules in the htaccess file in your server root directory.

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
...