简体   繁体   中英

How to get variable in query string without using “?”

I'm trying to use GET to capture member IDs in a URL without using the ?name=variable. For instance, instead of using mydomain.com/folder?id=1234 I would like to use mydomain.com/folder/1234 and pick up the id "1234" in the code so I can pass it to another URL.

This page simply redirects by using: <iframe src="http://redirect_domain.com/folder/index.php" />

I have no control over the server I'm redirecting to, but need the variable passed to it. I can append the redirect URL with ?id=1234 ie <iframe src="http://redirect_domain.com/folder/index.php?id=1234" />

Their code will pick up the id by using <?php echo $_GET["id"]; ?> <?php echo $_GET["id"]; ?>

Thanks for your help.

You'll want to use .htaccess for this. Create a file called .htaccess and put it in the same folder mydomain.com is referencing. Put something like the following in there

RewriteEngine On
RewriteRule ^folder/(.*)$ folder?id=$1 [L]

Note: mod_rewrite must be enabled on apache, but it usually is.

Update: I've updated the .htaccess to reflect a local URL since you've made your desired functionality more clear. Using this .htaccess in conjunction with <iframe src="http://redirect_domain.com/folder/index.php?id=<?=$_GET['id']?>" /> should do the trick.

Create a .htaccess file and use something like this

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

I feel the need to write my own answer down. The following two lines in your .htaccess should do exactly what you are asking for:

RewriteEngine On
RewriteRule ^folder/(.*)$ redirect_domain.com/folder/index.php?id=$1 [L] 

According to the rewrite rule test site http://martinmelin.se/rewrite-rule-tester/ , when I use an input of folder/1234 , it is redirected to redirect_domain.com/folder/index.php?id=1234 which is exactly what you asked for. There is no need for the <iframe ... or any actual files in the /folder/ other than the .htaccess ...

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