-1

I want to hide page extension and $_GET value

http://localhost/dice_mpc/members.php?as=c4ca4238a0b923820dcc509a6f75849b
http://localhost/dice_mpc/home.php?as=c4ca4238a0b923820dcc509a6f75849b
http://localhost/dice_mpc/profile.php?as=c4ca4238a0b923820dcc509a6f75849b

Expectation result:

http://localhost/dice_mpc/members/c4ca4238a0b923820dcc509a6f75849b
  • You can't without tricks or redirection, .htaccess happens server-side, it can rewrite the URL so that the one applied in server differs from the one in the browser. You can rewrite `members` to `members.php` but if you type `http://localhost/dice_mpc/members` in browser, you'll have no parameter at all. One solution is to change $_GET parameters to $_POST, which are passed ouside of the URL – Kaddath Aug 30 '19 at 07:36
  • You can not “hide” the value of a GET parameter, if your script _needs_ that value in the first place. – misorude Aug 30 '19 at 07:44
  • what if something like this http://localhost/dice_mpc/members/c4ca4238a0b923820dcc509a6f75849b page name and id value – Ariel Divine Torreon Aug 30 '19 at 07:56
  • What if what? Ask a proper question. – misorude Aug 30 '19 at 08:33

1 Answers1

1

I couldn't find the good duplicate (there are tons of questions about this) and in a good mood, so here we go:

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^dice_mpc/(.+)/(.+)$ dice_mpc/$1.php?as=$2 [QSA,L]
</IfModule>

DETAILS:

  • RewriteCond %{REQUEST_FILENAME} !-d will prevent rewriting if you access to an existing folder
  • RewriteCond %{REQUEST_FILENAME} !-f will prevent rewriting if you access to an existing file
  • RewriteRule ^dice_mpc/(.+)/(.+)$ dice_mpc/$1.php?as=$2 [QSA,L] will rewrite only URLS of format {domain}/dice_mpc/{A}/{B} so it won't redirect if {B} is not there. {A} will be converted to {A}.php and {B} to ?as={B}
  • QSA means query strings will be preserved ({domain}/dice_mpc/{A}/{B}?test=1 will be rewritten to {domain}/dice_mpc/{A}.php?as={B}&test=1), and L means rewriting will stop here
  • Don't forget to change your links to the new format, and be aware it may interact if you have other rewrite rules in your .htaccess

If this is not exactly what you want, please edit the question itself.

Kaddath
  • 5,273
  • 1
  • 7
  • 23