can I pass variables through URL while using htaccess RewriteRule? - php

I was wondering if it is possible to pass another variable through URL while already passing a variable using .htacces?
for example,
RewriteRule ^browse/([a-zA-Z-]+)/?$ browse/index.php?brand=$1
lets say, website.com/browse/index.php?brand=louis-vuitton
was rewritten into website.com/browse/louis-vuitton
is it possible for me to use another variable like this:
website.com/browse/louis-vuitton/?sort=name

Yes it is, you simply add [QSA] After the rewrite condition
RewriteRule ^browse/([a-zA-Z-]+)/?$ browse/index.php?brand=$1 [QSA]
http://wiki.apache.org/httpd/RewriteFlags/QSA

Related

QUERY_STRING only displays first parameter

I can only access my 1st URL parameter. I think it is because I have ampersands both in my RewriteRule as well as my QUERY_STRING. How can I access each parameter correctly in my PHP script?
I am making a request to
http://example.com/subdir/api/transport/1/car?type=fast&color=blue"
In my .htaccess file I have the following line:
RewriteRule ^(.+)$ index.php?q=%{REQUEST_URI}&params=%{QUERY_STRING}&api=$1 [L]
In my PHP, displaying json_encode($_GET) produces:
{"q":"\/subdir\/api\/transport\/1\/car","params":"type=fast","color":"blue","api":"transport\/1\/car"}
You will notice that the "color":"blue" no longer has an equal sign and is not contained within "params".
If I therefore display `json_enocde($_GET['params']) all I get is the following:
"type=fast"
What can I do to acquire the full set of parameters? Is it an issue with my .htaccess or my PHP file?
You don't need to use %{QUERY_STRING} in target by using QSA flag that appends existing query string to target if you're modifying query parameters.
You can just use your rule as:
RewriteRule ^(.+)$ index.php?q=%{REQUEST_URI}&api=$1 [L,QSA]
Well, if you think about the transformation that takes place:
index.php?q=%{REQUEST_URI}&params=%{QUERY_STRING}&api=$1
becomes:
index.php?q=/subdir/api/transport/1/car&params=type=fast&color=blue&api=transport/1/car"
So, of course you are getting that for your $_GET.
What I would do is change the rewrite to:
index.php?q=%{REQUEST_URI}&%{QUERY_STRING}&api=$1
And then in your code you will get:
{
"q":"\/subdir\/api\/transport\/1\/car",
"type": "fast",
"color":"blue",
"api":"transport\/1\/car"
}
If you want the params, you can then just remove the q and api keys from your $_GET.

Dynamic URL alongside URL rewrite

I am trying to have a URL like the following:
http://example.com/product/category/name?ref=title
Where there is a URL Rewrite AND (at least 1) ?attribute=value at the end of the URL.
How do I get the value of that attribute with the '?' before it? I have tried just using the basic $_GET[''] to get the value, but it doesn't seem to work.
Here are a few ideas:
RewriteRule /page /index.php?attr=val [QSA]
You can also use %{QUERY_STRING} variable in the rewrite rule.

url rewriting and pass the form's values

I'm having e commerce website in php. And now moving to fancy urls, url rewriting.
for that i've variable length of parameters with optional variables too.
for example,
http://example.com/product.php?page=e&id=23&color=blue&size=xxl&width=200&height=100
Here, the parameters are optional That means, some variables may or may not participage in the url except id.
here, page=e is fixed and it is not dynamic. ( the reason behind is i've other rewritten rules like ^categories, ^toys etc..
and re-written url should be one of these,
http://example.com/e/23/color-blue/size-xxl/
http://example.com/e/26/color-black/width-500/height-900/
http://example.com/e/56/type-shirt/color-white/height-345/size-xl/
i've tried below with no luck,
RewriteRule ^e/([^/.]+)/?$ product.php?page=e&url=$1 [L,NC,QSA]
i'm getting all $_GET values like this,
http://example.com/product.php?page=e&id=23&color=blue&size=xxl&width=200&height=100
but i'm trying to achieve like this,
http://example.com/e/23/color-blue/size-xxl/width-200/height-100
how can i pass the queries with slashes and not &'s.
that main idea behind above last url is to process the whole fancy url into simple one in php,
and then use those variables in the page script. is there any better solution than this ??
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^(e)/([0-9]+)/([^-]+)-([^/]*)(/.*)?$ product.php$5?page=$1&id=$2&$3=$4 [L,QSA]
RewriteRule ^(product\.php)/([^-]+)-([^/]*)(/.*)?$ $1$4?$2=$3 [L,QSA]

add variable in rewrite url

I got a rewritten url that looks like this:
http://www.mysite.com/users/login/
it's rewritten from http://www.mysite.com?module=users&class=login
The thing is: for the login, I want the return url.
I thought i could just do this:
http://www.mysite.com/users/login/?returnurl=http://www.mysite.com/whatever/url/we/are/at
but when I var_dump $_GET all I get is module and class.
How can I have the returnurl as well?
Add the [QSA] flag to your rewrite rule. Example:
RewriteRule /pages/(.+) /page.php?page=$1 [QSA]

How do I remove a variable name from a URL using htaccess?

So basically I want users to be able to go to my website with a URL of something like /45678, instead of having to use /?p=45678, so really I just want to remove the variable name. I've tried using mod_rewrite, but it seems that is only for removing the name when the page is visited.
Here is the current code:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^p=([0-9]+=$
RewriteRule ^/$ /%1 [R]
Simply change all of your links to /45678 rather than ?p=45678. Or did I misunderstand you completely? Because what I got from your post is that it works properly, unless you manually access the ?p=45678 where as it stays as ?p=45678.
EDIT:
This is what I am using for http://www.madphp.org/dev/, give it a go, works like a charm for me (it also removes the index.php part). To access your now cleaner URL you would simply explode the $_SERVER['PATH_INFO'] variable to get all of the required parameters within your PHP script.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Have you set up mod_rewrite correctly? If so, you can use variables like simple $_GET variables, in this case you must access $_GET['p'] in PHP.
I did this without using .htaccess, but it does query a database. I wrote this a while ago so it uses PEAR DB, adjust to your database/connection method. I'll just copy my code and let you figure out what changes you need.
$db=connect_db();
$name=substr($_SERVER['PHP_SELF'], 20);
$name=strtolower($name);
$id=$db->getone("select id from user where login='{$name}'");
header("Location: /dragonart/profile?user=" . $id);
If you store your information in a database this may be a nice alternative. The downside is that the the URL is not rewritten and the user is ultimately sent to a page with ending in a $_GET variable.
edit:
Just realized that using my method a simpler method can be used for the answer. Since my solution was used to find the id of a user using their username and then send someone to their profile (which requires the id) a better solution would be something like:
$var=substr($_SERVER['PHP_SELF'], $length);
header("Location: /path/to/page?p=".$var);
where $length is the usual length of the URL without the variable at the end.

Categories