hide incoming querystring with htaccess - php

I want that anybody who comes to my site
site.com?affiliate=a12b345c67
hides the affiliate querystring completely
I'm sure it's something like this, but nothing happens
RewriteRule ^/?affiliate= / [L,R=301]

To strip affiliate= query string from your URLs, place this rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^affiliate=[^&]+ [NC]
RewriteRule ^ %{REQUEST_URI}? [L,R=301,NE]

First of all you need to charge the URL from which users are coming to your website. Change it to:
site.com?/a12b345c67/
.htaccess cant change your URL structure on the fly.
Let me know if its ok.
Regards.

you could save the incoming $_GET parameter(if exists) to a session(or cookie, or whatever persistent storage) and reload the page.
session_start();
if(isset($_GET['affiliate']){
$_SESSION['affiliate'] = $_GET['affiliate'];
header('location: site.com');
}
you could then use the affiliate token from the session,cookie or whatever persistent storage

Related

htaccess rewrite url rule trouble detect $_GET isset

RewriteEngine On
RewriteRule ^about/([^/.]+)/?$ about.php?id=$1 [L,QSA]
I have a page use htaccess rewrite rule for url
/about/33
the page require $_GET['id'] to fetch db
however i have trouble to detect GET variable isset
if(isset($_GET['id'])){fetch data...}else{header("location:...");}
if user only enter /about/ without $GET['id'] the page will not found instead run header("location");
is anyway to solve this?
try this:
RewriteRule ^about/([^/.]*)/?$ about.php?id=$1 [L,QSA]
Put * instead of +

PHP code for anti hotlinking

In our sites we are doing a image protection section. So as a part of image protection we need
provide antihotlinking for images.In our site we are showing the image using a generated url.
For example in our site the image source is like: image_file.php?type=image&w=10&h=10&i=12
(this only a fake url for example purpose).
So using this url we need to show image in our site and at the same time want to prevent it from hot linking is there any way for prevent hotlinking?
If you can utilize the .htaccess method then great, additionally, as I said in my comment, a 100% fool proof way is to utilize base64 encoding. When you are displaying images, you can use this code to convert them to base64:
<?php
$imagedata = file_get_contents("/path/to/image.png");
$base64 = base64_encode($imagedata);
?>
<img src="data:image/jpeg;base64,<?= $base64; ?>" />
Also, if you want to get really creative, you can "RAT" the hotlink "thieves" out by displaying an alternative image using your .htaccess file... do this like so:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain.com/.*$ [NC]
RewriteRule \.(gif|jpg|png)$ http://www.mydomain.com/dontstealmystuff.png [R,L]
just make sure dontstealmystuff.png is available on the server
basic .htaccess example
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yourdomain.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F,L]
above allows a blank REFERER (like me).
this does not:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yourdomain.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F,L]
there are quite a few variations you can find, may need to play around a bit to find what is best for you.
Image hotlinking is usually detected by referer, but it won't work when:
user has turned off referer sending in his browser (I have this for privacy purposes)
page is viewed via HTTPS (browser shouldn't send referer data).
You'll block your actual users from viewing images.
Consider using sessions / cookies when dealing with this problem. You'll have to pass every image via php script then.
Generally speaking the proper way to do this is in something like an .htaccess file with a command such as:
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?somesite\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule .*\.(jpe?g|gif|bmp|png)$ http://i.imgur.com/aNbhd.jpg [L]
However to do this in PHP it's basically the same. All you do is verify that $_SERVER['HTTP_REFERER'] starts with the URL for the page. However it's possible to spoof the HTTP_REFERER so it's not going to be 100%. However the user has to do this (an external site pretty (mostly...) much can't spoof this), so it will prevent other sites from embeding your images without placing your site in an iframe or some other hoopla.
Another way, and probably the safest though it's going to be the hardest on the server, is to use the $_SESSION variable to pass a token/flag around, then check the token.
session_start();
$_SESSION["allow_images"] = true;
Then on the PHP page that gets the image for them:
if($_SESSION["allow_images"])
{
//Send some pics!
}
However this only works if the user hasn't been to your site recently enough to not have their own session still active.
You can try checking the value of $_SERVER['HTTP_REFERER'] against a known value, but as the documentation states, that can be spoofed. It might help against the common case, though.
in image_file.php use http_referer for this.
$ref = isset($_SERVER['HTTP_REFERER'])? $_SERVER['HTTP_REFERER']: "";
if ($ref != "" && strpos($ref,'http://www.yourdomain.com/')===0)
{
//the request for this image is coming from some other domain, so take appropriate action
}
else
{
//do whatever logic you are currently using to show the images
}
Find a full-blown solution here: http://safalra.com/programming/php/prevent-hotlinking/

Can't access post or get from a php page after redirecting

I'am redirecting about 100 hmtl pages to a single PHP page (example.php) using .htaccess. It is working perfectly.
I've pagination on that page (example.php) but I am using the original HTML page URL (example.html?page=2&limit=20)
so example.html, example1.html, example2.html, example3.html are all redirecting to example.php.
The address bar is still showing ".html" URL but due to .htaccess redirection the example.php is rendering.
when is click on a pagination link (example.html?page=2&limit=20) the browser address bar shows correct .html URL and query string.
I've tried to get the values of page, and limit using $_GET and $_REQUEST in (example.php) but i am not successful.
Please help me in reading the (example.html?page=2&limit=20) query string parameeters .
Edit Code ported from comments:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !^page-(.*)$
RewriteRule ^page-(.*)$ size-content.php?sef=$1 [L]
Add the QSA flag, which means "query-string append" to be sure the existing query string is ported into the rewritten URL.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !^page-(.*)$
RewriteRule ^page-(.*)$ size-content.php?sef=$1 [L,QSA]
.htaccess modify your server configuration.
if you are making redirection then you change your request.
Try mod_rewrite if you are using Apache of course.
RewriteEngine On
RewriteRule ^example\.html\?(.*) example.php?$1
Mod rewrite is module to Apache. It is not allowed on most free hostings.
Yasir - you can resolve this problem by two ways:
1) Re-write rules for .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !^page-(.).html(.)/(.)$
RewriteRule ^page-(.).html(.)/(.)$ size-content.php?sef=$1&page=$2&limit=$3 [L]
This rule will handle: page-example.html?page=2&limit=20
I hope - you will easily understand the above rule.
Note: Keep one thing in your mind that every link should be in same pattern if you change rule in htaccess.
2) You can resolve this problem on your "size-content.php"
Suppose page-example.html?page=2&limit=20
$_GET['sef'] = example.html?page=2&limit=20 [according to you .htaccess]
Now you can parse this string via explode function
Thanks

PHP / htaccess redirect via GET

I want:
sub.domain.com to load index.php
sub.domain.com?s=custom to load /custom/index.php
I'm doing this right now:
In .htaccess:
RewriteRule ^(custom)/?$ index.php?subject=$1
and in index.php I have this:
if($_GET['s'] == 'custom') {
header( 'Location: http://sub.domain.com/custom/index.php' ) ;
}
... but is it possible to do the redirect via htaccess itself depending on the GET variable?
Thanks!
Yes, you could check query string with RewriteCond
RewriteCond %{QUERY_STRING} s=custom
RewriteRule ^(.*?)$ custom/index.php [L]
With this, it redirects all requests with existing "custom" get parameter to index.php, and this can be extended :)
Idea: http://statichtml.com/2010/mod-rewrite-baseon-on-query-string.html
You probably want to use "RedirectMatch"
RedirectMatch ^sub.domain.com?s=custom$ /custom/index.php
Maybe youll need to play with the regex abit

How can I forward a dynamic URL to twitter using .htaccess or PHP?

I need to rewrite a dynamic URL. The content myurl and mytext is always different and should be insert in the "text" and "url" from the Twitter string.
This is the string:
http://example.com/share/?myurl=http%3A%2F%2Fwww.example.com&mytext=helloworld
/* forward to: */
http://twitter.com/intent/tweet?related=Example%3Aname&text=helloworld&url=http%3A%2F%2Fwww.example.com&via=example
How can that be made? (I browsed the .htaccess suggestions but couldn't find a solution for my specific problem.)
Does your server support PHP?
You could just put something like the following in a PHP file:
Header("Location: http://twitter.com/intent/tweet?related=Example%3Aname&text=$_GET['mytext']&url=$_GET['myurl']&via=example");
It will be more efficient to handle it with rewrite Rules.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^myurl=(.*)&mytext=(.*)$
RewriteRule ^(.*)$ http://twitter.com/intent/tweet?related=Example%3Aname&text=%2&url=%1&via=example [R,L]
If you want this to be permanent redirect then replace R with R=301.
With permanent redirects , these links will always be redirected by browser and your server will have to deal with less traffic if that is desired.

Categories