.Htaccess Rewrite Get Variables

.htaccess rewrite GET variables

Basically what people try to say is, you can make a rewrite rule like so:

RewriteRule ^(.*)$ index.php?params=$1 [NC, QSA]

This will make your actual php file like so:

index.php?params=param/value/param/value

And your actual URL would be like so:

http://url.com/params/param/value/param/value

And in your PHP file you could access your params by exploding this like so:

<?php

$params = explode( "/", $_GET['params'] );
for($i = 0; $i < count($params); $i+=2) {

echo $params[$i] ." has value: ". $params[$i+1] ."<br />";

}

?>

.htaccess rewrite with GET variables

If your URL with query string is working fine then it most probably it means your .htaccess rules are not working, usually we give user friendly URLs to users in your case it should be www.example.com/user/david and internally it should route to www.example.com/user.php?username=david. Please try following Rules in your .htaccess file.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/user/(.*)$
RewriteRule ^.*$ /user.php?username=%1 [QSA,NC,NE,L]

NOTE: if possible and you are not on prod systems then you could try to restart your Apache service too, though it's not necessary but sometimes it may be required. Also use user friendly URL as mentioned above in browser make sure you clear the browser cache before hitting it.

How to catch GET variables in a htaccess Rewrite Rule

You can't match the query string using the RewriteRule pattern, you need to use a RewriteCond directive and check against the QUERY_STRING server variable. However, in this instance, you MUST also ensure that MultiViews is disabled (otherwise orders.php will be called without any URL parameters).

Try the following instead:

# MultiViews MUST be disabled
Options -MultiViews

# Rewrite URL with query string
RewriteCond %{QUERY_STRING} ^token=([^&]+)&status=([^/]+)$
RewriteRule ^orders/$ orders.php?lang=it&token=%1&result=%2 [L]

Note the use of the %1 and %2 backreferences (as opposed to $1 and $2) that get the values from the captured groups in the preceding condition, as opposed to the RewriteRule pattern.

.htaccess: GET variables are lost in rewrite

You need the "QueryString Append" option:

RewriteRule ^(.*)$ index.php?route=/$1 [QSA,L]

Edit: Added @DonSeba's contribution, because it is correct.

Rewrite GET variables php

you can get the variable values automaticly by this method

RewriteEngine On
RewriteBase /
RewriteEngine On Options All -Indexes RewriteBase /directoryname/ RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

############### SEO ##########################

#
http://www.example.com/hello/booboo/ it takes the url after .com/
RewriteRule ^(.*)$ getme.php?url=$1 [QSA,L]

for example if i enter http://www.example.com/hello/bobo/

it is going to replace and take "/hello/bobo/" part now you can use this in .htacces file Also if you want to redirect to another page and filter the variable value you should modify $1 because all data in this.

edit: in that example i get url after my domain and i redirect to get.php also you can divide the url using split method by "/" Let's see the get.php page to understand this method

getme.php:

<?php

//we redirect to get in url=$1 so our get method name is url
$parca = explode("/", $_GET["url"]); //and we divided by slash the url.

echo $parca[0];//this is first part "/hello/
echo $parca[1];// and this is second part "/booboo/;
?>

URL Rewrite GET parameters

Add this to your .htaccess in your web root / directory

RewriteEngine on
RewriteBase /

RewriteRule ^home$ index.php?page=home&%{QUERY_STRING} [NC,L]

If you want this to work for all pages i.e. /any-page gets served as index.php?page=any-page then use

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d # not a dir
RewriteCond %{REQUEST_FILENAME} !-f # not a file
RewriteRule ^(.*)$ index.php?page=$1&%{QUERY_STRING} [NC,L]


How do these rules work?

A RewriteRule has the following syntax

RewriteRule [Pattern] [Substitution] [Flags]

The Pattern can use a regular expression and is matched against the part of the URL after the hostname and port (with the .htaccess placed in the root dir), but before any query string.

First Rule

The pattern ^home$ makes the first rule match the incoming URL www.website.com/home. The %{QUERY_STRING} simply captures and appends anything after /home? to the internally substituted URL index.php?page=home.

The flag NC simply makes the rule non case-sensitive, so that it matches /Home or /HOME as well. And L simply marks it as last i.e. rewriting should stop here in case there are any other rules defined below.

Second Rule

This one's just more generic i.e. if all of your site pages follow this URL pattern then instead of writing several rules, one for each page, we could just use this generic one.

The .* in the pattern ^(.*)$ matches /any-page-name and the parentheses help capture the any-page-name part as a $1 variable used in the substitution URL as index.php?page=$1. The & in page=home& and page=$1& is simply the separator used between multiple query string field-value pairs.

Finally, the %{QUERY_STRING} and the [NC,L] flags work the same as in rule one.

htaccess rewrite get parameter in subdirectory and retrieve with php

You may try this rule in site root .htaccess:

RewriteRule ^(subdir/[\w-]+)/([\w-]+)/?$ $1.php?param=$2 [L,NC,QSA]
  • Pattern needs to match file part also after subdir/
  • Use a capture group and back-reference to avoid repeating same text
  • Leading / in regex pattern is not matched in .htaccess
  • This assumes there is no other .htaccess in subdir/.

Htaccess rewrite URL with passing $_GET parameters

You may use this rule in your Dir/childDir/.htaccess:

RewriteEngine On

# rewrite /Dir/childDir/12345 to /Dir/childDir/index.php?id=12345
RewriteRule ^(\w+)/?$ index.php?id=$1 [L,QSA]


Related Topics



Leave a reply



Submit