Goes without saying, I know completely nothing about HTACCESS. I'm coding a referral system in PHP, and it all works, although it stops working when I use HTACCESS to make the URLs pretty. Example:
From
http://localhost/ref.php?referral=1
To
http://localhost/ref/1
Now the code works and all, it just stops working when I use HTACCESS, I remember reading from somewhere, if I'm not wrong, that you can't use the _GET function from a URL which has passed through HTACCESS? None the less, I've tried this HTACCESS rule, but it doesn't seem to work.. It redirects, as per the code it told to do when it drops the cookie, but it's not actually dropping the cookie when I use the shortened URL.
RewriteRule ^ref/([^/]+)(|/)$ /ref.php?referral=$1
The code of the script, if it helps understand the issue..
if (isset($_GET['referral']))
{
$value = $_GET['referral']; // Let's set the cookie value
// Drop the cookie, let it expire in an hour.
setcookie("HHRefCookie", $value, time()+3600);
// Redir them to register page
header("Location: " . WWW . "/register");
}
From my understanding, this should be working? Should it not. Feedback would be appreciated. And guidance towards the right direction would be much appreciated!
Did you look at the status code returned by the server? I can't see how the rewrite rule you've provided would match the URL schema you are using (i.e. I'd expect the server to return a 404).
Try:
RewriteRule ^/ref/(.*)$ /ref.php?referral=$1 [L]
(and if this is Apache 2.4 then use [END] rather than [L])
Related
I just saw this somewhere, and I'm interested on it, and can't seemed to find it anywhere or I just used the wrong words to search for.
Well I saw this link,
http://splur.gy/r/QqVYf/r/2tgNklHgmVK
and when I clicked it, I got redirected to other page which called
https://www.facebook.com/xxx.xxx?sk=app_xxxx
Anyone knows how this thing was made? or just a little hint to start off?
A help would be nice. :)
These are done with RewriteRule, a simple Google search willgive you mroe details.
In short, the URL will be broken down sorta like this: (Line 1, URL part, Line 2, PHP relative.
http://splur.gy
http://splur.gy/index.php
r
$_GET['var_1']
QqVYf
$_GET['var_2']
r
$_GET['var_3']
2tgNklHgmVK
$_GET['var_4']
The RewriteMod will take the URL as setup in the above format, and pass the varialbes to a script. It is another way of posting variables in the URL.
As you see above: stackoverflow.com/posts/15182831, does not actually have a file named posts/15182831, it is simple used as a variable, passed to a script which queries that database, and spits out results based on what the script says.
You will need to have a server that will allow you to rewrite requests so you can redirect all requests to a single script. If you are running Apache, you would create an .htaccess file with something like this in it:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^r$ /redirect.php [L,QSA]
RewriteRule ^r/(.*) /redirect.php?__q=/$1 [L,QSA]
</IfModule>
Then if you go to http://yourdomain.com/r/234243/adsfsd, the request will be sent to the script /redirect.php and '234243/adsfsd' will be passed as the GET paramiter 'q'.
Then you would create a file called redirect.php that would process the request and then redirect the user. It might look somthing like this:
<?php
$redirection = process_to_determine_location_from_query( $_GET['q'] );
header( 'Location: {$redirection}' );
?>
It's called a redirect. You can do it in PHP with this code:
<?php
header('http://example.com');
Another thing that might have happened is that the link you saw was not the actual link you follow when you click. It's as simple as doing this:
example.com
Anyone could do that.
http://www.google.com/
It has nothing to do with PHP.
this is my first post so go easy on me.
Basically I am doing some rewrites in my htaccess file to change my made up search friendly URLs into actual URLs, and for the most part they are working. For instance this:
http://www.negativeworld.org/7849/news/nintendo-download-for-may-24-2012
Will turn into this:
http://www.negativeworld.org/article.php?id=7849
Just fine... IF that article exists. If the article doesn't exist, the php code uses this:
header("Location: boarderror.php");
exit;
To bring the user to boarderror.php. This works fine if it the user gets there directly on article.php and the id is bad, but when I am trying to do the htaccess redirect from a search friendly url and the id is bad, the htaccess redirect just hangs for awhile before giving me this message: "The page isn't redirecting properly".
What I want is for it to go to my boarderror.php page when there is a bad id. So basically I want my htaccess page to take a server friendly URL, switch to the true URL, and well... just let go at that point, and the PHP will take it from there. Here is my htaccess line that does the switch:
RewriteRule ^([0-9]+)/(news|review|editorial|podcast)/(.*)$ /article.php?id=$1 [L]
What am I doing wrong? (BTW I realize that if I set up all of my search friendly URLs correctly there should never be a bad id anyway, but I want to be on the safe side...)
Your thoughts aren't wrong. For a wrong ID there is a double redirection which is OK. The problem is how the second redirection happens. Try
header("Location: http://www.negativeworld.org/boarderror.php");
or
header("Location: /boarderror.php");
With your redirection the browser is trying http://www.negativeworld.org/9999/news/boarderror.php (being 9999 the wrong ID) which falls in an endless redirection loop that the browser cuts after 10 tries.
The redirect rule is fine, the issue is in your header function call. When you only provide a file name, the header redirect will send the user to the file in the same folder, much like creating an html link using only the filename.
Let's say i try to load http://www.negativeworld.org/99999/news/nintendo-download-for-may-24-2012 and that id is invalid. In this case it would send send the user to http://www.negativeworld.org/99999/news/boarderror.php which triggers the redirect again and gets stuck in an infinite loop (or would if the browser wasn't smart enough to stop requesting the same URL over and over again).
Per RFC 2616 the location header should provide an absolute URI, so you should do something like this:
header("Location: http://www.negativeworld.org/boarderror.php");
exit;
So I have errors which are passed by the url, for example
index.php?error=nojs
will then be parsed by PHP to return an error message, for example: Please enable Javascript
I'm using the following line in my .htaccess to make the url easier to manage
RewriteRule ^ERROR_(.*)$ index.php/?error=$1&%{QUERY_STRING} [L]
It makes my URL look like this:
site.com/ERROR_nojs
The problem is, this only works for the root,
index.php?error=nojs works fine however
test/index.php?error=nojs does not?
So how can I convert the variable for every directory?
Thank you. (My original script handles hundreds of errors and filters out ones that might be useful to output to the user. It would be stupid to redirect them to the index just so they can see a small with an error message in it)
EDIT:
as Shai Mishali pointed out removing the '^' before ERROR did the trick.
RewriteRule ERROR_(.*)$ index.php/?error=$1&%{QUERY_STRING} [L]
But I forgot to tell you I have another variable ?page=
I need get that vairbale and add it to the url in order for this to work..
e.g:
index.php?page=home&error=nojs
= site.com/home/ERROR_nojs
so
www.site.com/?page=home&error=nojs = www.site.com/home/ERROR_nojs
or
www.site/?page=about&error=unknown = www.site.com/about/ERROR_unknown
I'm pretty sure your problem is your rule is looking for something that starts with ERROR (the ^ sign) .
/ERROR starts with error, which works in your root , but
/tests/ERROR starts with tests , so it won't recognize it.
Try removing the ^ sign and see what happens.
Shai.
You can use below code
RewriteRule ^([a-z0-9A-Z]+)/([a-z0-9A-Z]+)$ ./index.php?page=$1&error=ERROR_$2 [NC]
Your URL site.com/home/ERROR_nojs will treated as index.php?page=home&error=nojs and you can get the values by GET method. For more info
I have a directory named "goto" and a file inside called index.php. Currently the following is inside the index.php file:
<?php
$url = $_GET['url'];
header("Location: $url");
?>
At the moment to redirect to another URL I have to type this into the address bar:
http://mysite.com/goto/?url=http://google.com
I would appreciate it if you could tell me how I could change that URL so that I could redirect the user to a website by typing this into the address bar:
http://mysite.com/goto/http://google.com
Use mod_rewrite and .htaccess to rewrite http://mysite.com/goto/http://google.com as http://mysite.com/goto/?url=http://google.com
RewriteEngine On
RewriteRule ^goto/(.+)$ /goto/?url=$1 [L]
Depending on your server configuration you may need to include a / in your rewrite path (i.e., ^/goto/(.+)$).
Unless you want to become a malware hub, I would wholeheartedly recommend you not doing this.
If you wish to allow redirect in such a manner, using http://mysite.com/goto/google and then work out the domain from a whitelist of available, allowed, destinations.
You will need to parse the data which could be a little tricky because you have to differentiate the difference between your URL and the other URL.
My suggestion is to not do so because the second that header is launched you will not see the url and it be better for you to just pass it as a get statement or a post.
EDIT
If you're determined then parse_url() is what you want. :)
#ide's method would work ... but you could also have the PHP script examine $_SERVER['PATH_INFO'], which is how that part of the URL would get passed to the CGI script.
(although, if there's a question mark in there, you'll also have to either make sure it's URI encoded, or also get the QUERY_STRING; you'll also lose any part after a hash, but you'd have the same problem with your current scheme)
suppose we have the following PHP page "index.php":
<?
if (!isset($_GET['req'])) $_GET['req'] = "null";
echo $_SERVER['REQUEST_URI'] . "<br>" . $_GET['req'];
?>
and the following ".htaccess" file:
RewriteRule ^2.php$ index.php?req=%{REQUEST_URI}
RewriteRule ^1.php$ 2.php
Now, let's access "index.php". We get this:
/index.php
null
That's cool. Let's access "2.php". We get this:
/2.php
/2.php
That's cool too. But now let's have a look at "1.php":
/1.php
/2.php
So... we ask for "1.php", it silently redirects to "2.php" which silently redirects to "index.php?req=%{REQUEST_URI}", but here the "%{REQUEST_URI}" seems to be "2.php" (the page we're looking for after the first redirection) and the $_SERVER['REQUEST_URI'] is "1.php" (the original request).
Shouldn't these variables be equal? This gave me a lot of headaches today as I was trying to do a redirection based only on the original request. Is there any variable I can use in ".htaccess" that will tell me the original request even after a redirection?
Thanks in advance and I hope I've made myself clear. It's my first post here :)
I'm not sure whether it will meet your needs, but try looking at REDIRECT_REQUEST_URI first, then if it's not there, REQUEST_URI. You mention in your comment to Gumbo's answer that what you're truly looking for is the original URI; REDIRECT_* versions of server variables are how Apache tries to make that sort of thing available.
Just change the order of the rules and it works:
RewriteRule ^1\.php$ 2.php
RewriteRule ^2\.php$ index.php?req=%{REQUEST_URI}
Or use just one rule:
RewriteRule ^(1|2)\.php$ index.php?req=%{REQUEST_URI}
Well I guess I solved the problem. I used the %{THE_REQUEST} variable which basically contains something like this: "GET /123.php HTTP/1.1". It remains the same even after a redirection. Thanks everyone for your help! :)