简体   繁体   中英

How to handle GET-Parameter in PHP when modrewrite is activated?

I've a little problem. I use modrewrite to load language-content dynamically into webpage...

example:

www.mydomain.xyz/en (the english version) www.mydomain.xyz/fr (the french version) ...

My .htaccess:

 RewriteRule ^en$ index.html?lang=en

But:

www.mydomain.xyz/en?foo=bar

and

<?php
 if(isset($_GET["foo"])) {
   echo "Yeah! There is some stuff!";
 }
 ?>

doesn't work... the GET-Parameter is ignored. What I have to do?

  • it works, with www.meinedomain.xyz/en.html?foo=bar but I don't want to display any Extensions.

Use QSA flag:

RewriteRule ^en$ index.html?lang=en [L,QSA]

To make this work for en and fr use this:

RewriteRule ^(en|fr)/?$ index.html?lang=$1 [L,QSA]
  • QSA (Query String Append) flag preserves existing query parameters while adding a new one.

Simply add [QSA] to your rewrite rule:

RewriteRule ^en$ index.html?lang=en [QSA];

https://wiki.apache.org/httpd/RewriteQueryString

You need the [QSA] instruction right after your rewrite rule

http://httpd.apache.org/docs/current/mod/mod_rewrite.html

QSA is "query string append" and preserves query strings on rewrite

I'd also recomment using (/|$) instead of just $, or calling yourdomain.com/ensomefile.php would also result in rewriting

RewriteRule ^en(/|$) index.html?lang=en [QSA]

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