mod rewrite with fixed string between two dynamics - php

I've got an URL like this:
filter.php?gender=male&fid=1&sid=x
and I want to use mod_rewrite to rewrite it like this:
/Male/Apparel/Jackets
as an example...
I already tried the following but itonly puts the first var (male) to the query string and skips the second one (example: jackets)
RewriteRule ^([a-zA-Z]+)/Apparel/^([a-zA-Z]+) filter.php?gender=$1&fid=1&sid=$2
I also tried the following syntax without success:
RewriteRule ^([a-zA-Z]+)/Apparel/([a-zA-Z]+) filter.php?gender=$1&fid=1&sid=$2
What have I done wrong? Isn't it possible to put an exact string between two dynamic string or did I just use a wrong syntax?

Related

Apache mod_rewrite: rewrite a path into a query string of an existing file

I am creating a simple redirect tool for myself in PHP. I'm going to store codes that map to their respective URLs in an associative array, and then redirect anybody who inputs this code to their URL.
I am using query strings to GET the input (the name of the variable and the value, which is the code), and I already know how to make the redirect part work with PHP. The issue is, I don't want my users to have to type in a question mark, the name of the variable, and then an equals sign. I would like for them to simply type in a path that will be converted into a query string, so that PHP can process the redirection.
I was wondering how I could turn a path (an arbitrary URL path) into a query string of an existing file. I was able to find the opposite task quite easily on Stack Overflow (.htaccess - Rewrite query string and redirect to directory) but I want to be able to pass any arbitrary URL path (that does not exist in the directory) into an existing file's query string. I found an example of what I wanted (URL rewrite for converting path to querystring), but it was for multiple query strings and in this case, I only need one. I don't think the last question I referenced was successfully answered either.
How can I redirect
https://www.example.com/r/blahblahblah
into
https://www.example.com/r/index.php?r=blahblahblah
using .htaccess, so that I can parse the query string in my PHP file?
Something like this:
RewriteRule ^(.*)$ index.php?r=$1 [NC,L,QSA]
On this URL:
example.com/r/blahblahblah
From index.php:
print_r($_GET);
Will return this:
Array ( [r] => blahblahblah )
(Edit to mention this assumes .htaccess is in the /r directory)
RewriteEngine On
RewriteRule ^r/([a-zA-Z0-9]+)$ /r/index.php?r=$1

Alter htaccess for $_GET utm_medium from dlvr.it

Dlvr.it puts utm_source and utm_medium in the URLs it generates. I am trying to access those in my PHP code but the page cannot get its value. The two values of utm_medium I'm looking to track are twitter or Facebook.
Example url that doesn't work: example.com/page/id?utm_source=dlvrit&utm_medium=twitter
Example that DOES work: example.com/page.php?id=xxx&utm_medium=twitter
Any one have any idea why I cannot access it or any experience with using dlvr.it generated URLs?
What I have:
$source = $_GET['utm_medium'];
echo $source;
I am getting the error: Undefined index: utm_medium
EDIT: My htaccess is structured so the page is displayed as /page/xxx (xxx being an id number) so the parameters from dlvr.it are not active. How can I go about altering my htaccess to gain access to these parameters?
Add the flag [QSA] to your RewriteRule – stands for Query String Append, and will append the original query string to the address that you rewrite to internally.
http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_qsa:
“When the replacement URI contains a query string, the default behavior of RewriteRule is to discard the existing query string, and replace it with the newly generated one. Using the [QSA] flag causes the query strings to be combined.”

Using two GET variables in .htaccess (having problems outputting)

I'm currently trying to add a second get variable to an existing htaccess rule. I have little experience with the htaccess but using what expressions I know, I think it's missing one element to ensure the second variable is outputted correctly.
The domain (as the user sees it);
http://domain.com/london-area-info/?src=go
The rule i'm trying to apply;
RewriteRule ^(.*)-area-info/(.*)$ regioncc.php?region=$1&src=$2
The resulting output URL;
http://domain.com/regioncc.php?region=london&src=
I appreciate I'm probably overlooking something but looking around I haven't found an explanation has to how to make sure the ?src=go is shown after the rewrite rule rather than in another directory like /xxx-area-info/src/ for example.
EDIT
I have also tried the following rule but wasn't sure if I was escaping correctly
RewriteRule ^(.*)-area-info/(\?src=?.*)?$ regioncc.php?region=$1&src=$2
I believe this is what you want:
RewriteRule ^(.*)-area-info regioncc.php?region=$1&%{QUERY_STRING}

Rewriting PHP Query to HTML using .htaccess

I am having a lot of trouble getting my php querys re-written to any sort of seo friendly url.
I want to get
http://example.com/provider_list/Dublin.html from the url below
http://example.com/provider_list.php?locationName=Dublin&checkIn_Date=09%2F07%2F2012&checkOut_Date=10%2F07%2F2012&extratype=1&noofextra=1&x=50&y=21
And the same for all other locations
e.g.
provider_list/Wexford.html
So if the query includes the "locationName=ANYLOCATION" it would re write it to this...
It shouldn't matter what the values are for the other names in the query string ...
Is this possible through .htacess?
All help gratefully recieved..
Try this:
RewriteEngine on
RewriteRule provider_list/(\w+).html /locationName=$1&checkIn_Date=09%2F07%2F2012&checkOut_Date=10%2F07%2F2012&extratype=1&noofextra=1&x=50&y=21 [L]
But this is not elegant solution with such long query string, is it necessary?

base64 encode is giving me strange results after using url rewrite

I have a really strange problem
ok the problem is thus
let say I have this url
http://www.example.com/?file=WFS010C.part3.rar&url=http://www.fileserve.com/file/vEpByp3/WFS010C.part3.rar
If I do base64_encode($_GET['url'])
the results is this
aHR0cDovL3d3dy5maWxlc2VydmUuY29tL2ZpbGUvdkVwQnlwMy9XRlMwMTBDLnBhcnQzLnJhcg==
after applying this rewrite condition
RewriteEngine On
RewriteBase /
RewriteRule ^file-(.*)-(.*)\.html$ index.php?file=$1&url=$2
The url is like this
http://www.example.com/file-WFS010C.part3.rar-http://www.fileserve.com/file/vEpByp3/WFS010C.part3.rar.html
now if I do base64_encode($_GET['url'])
the results is this
aHR0cDovd3d3LmZpbGVzZXJ2ZS5jb20vZmlsZS92RXBCeXAzL1dGUzAxMEMucGFydDMucmFy
**Note that the value of $_GET['url'] is exactly the same in both cases if printed without encoding!!!!
how come same string giving different results just after using rewrite ?**
Does anyone know whats the problem
You can use base64_decode to see whether they are same.
In fact:
The first one is
http://www.fileserve.com/file/vEpByp3/WFS010C.part3.rar
And the second one is
http:/www.fileserve.com/file/vEpByp3/WFS010C.part3.rar
You can see, they are not same.
You are rewriting your URl so the part after the second - is stored in url parameter. So in second case you receive url = http://www.fileserve.com/file/vEpByp3/WFS010C.part3.rar.html. Just like you had before.
What you are encoding is marked with bold:
With first url
http://www.example.com/?file=WFS010C.part3.rar&url=http://www.fileserve.com/file/vEpByp3/WFS010C.part3.rar
With second URL you are rewriting it to
http://www.example.com/index.php?file=WFS010C.part3.rar&url=http://www.fileserve.com/file/vEpByp3/WFS010C.part3.rar
UPDATE your second base64_encoded URL is decoding to http:/www.fileserve.com/file/vEpByp3/WFS010C.part3.rar. Note single / after http. Are you sure you have no typos in second URL?
First you need to urlencode the 'url' parameter. I'm guessing the parameter does not get correctly parsed because of invalid characters like '/' in it.

Categories