Use .htaccess to change the url PHP sees - php

I want to use htaccess to not only choose the script that processes the request, but also to change the request uri as php sees it. Can this be done?
For example:
RewriteRule /funstuff/ funstuff.php
...How can I change that RewriteRule or otherwise change my .htaccess file to get funstuff.php to think that the original request url was actually http://www.example.com/funstuff.php and not http://www.example.com/funstuff/?

The best I can see is
$_SERVER["SCRIPT_NAME"] or $_SERVER["PHP_SELF"]
(Both should be fine)
in conjunction with
$_SERVER["QUERY_STRING"]
to get the full request. There seems to be no ready-made REQUEST_URI for this.

Related

Generate URL Alias?? in PHP

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.

how to use $_GET with this url format site.com/extension/rar

I am seeing this url format at most websites.
site.com/extension/rar
I wonder how they get the value='rar' using $_GET.
What I know is that $_GET can be use in here
site.com/extension/index.php?ext=rar
Now I wanted to change my way of calling a variable.
I wanted to apply what most websites do.
How can I call variable in the former?
Perhaps this works to get the "rar":
$name = basename($_SERVER['REQUEST_URI']);
I most likely being done using .htaccess
It is an Apache module that allows you "rewrite" urls at the engine level based on your own set of rules. So basically it rewrites URLs on the fly.
So, in your example you could have a file named .htaccess with the following contents: (there may be other options)
RewriteEngine On
RewriteRule ^extension/([a-z0-9]+)$ somefile.php?extension=$1 [L]
Basically, you are saying: If someone is looking for a URL that looks like "extension/somenumbers-and-letters" then show the contents of "somefile.php?extension=whatever-those-number-and-leters-are".
Do a search on Apache mod_rewrite to find more information.

URL rewriting issue when using / in url pattern

I am having problem in URL rewriting using .htaccess file, problem is as below.
When I put below code in my .htaccess file on server
RewriteRule ^/?xyz http://google.com [R=301]
and I call url MY_WEB_DOMAIN.COM/xyza in browser it redirects me to google.com ( as expected )
And if I call url MY_WEB_DOMAIN.COM/xyz/abc I get 404 error..
isn't it should redirect to google on both case?
or something else is affecting my code with I use / (slash) in my pattern?
Thanks in advance.
arkascha is right. Use this:
RewriteRule ^/?xyz.* http://www.google.com [R=301]
the /? makes that first / optional, which is sort of asinine in this case. But the .* is what you want to catch anything matching /xyz-----
if arkascha posts that as an answer you should accept it.
I tested this and it works fine. If it doesnt work for you check what lines you have above this rule in htaccess. you may have a conflict.
Rewriting was working ok.
I checked headers on server side for rewritten URL and directly called url.
I found only one change.
in direct url It was containing redirect_url and request_uri were same.. for rewriten url both were different.
(I thought oxwall might be using request_uri to process.. where as it was not as I wanted to be..
So I just made them same for rewritten url in php. by placing line
$_SERVER['REQUEST_URI']=$_SERVER['REDIRECT_URL'];
And It is done. :)

PHP redirect to another website

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)

Detect if a page have been called via .htaccess RewriteRule in PHP

I am using .htaccess RewriteRule on a website I'm working on.
Here is a sample of my .htaccess
RewriteEngine on
RewriteRule ^about.htm$ /index.php?load=about&output=html [NC]
I would like to know if there is a way in my index.php file to detect
if the page have been called via a Rewrite or the user reached it
directly. I'm trying to avoid having to write some security check that
I am not even sure where to start.
If there is no way to make that "check" where should I start to secure
the file ?
My guess would be to make sure only load and output are
passed to the $_GET, make a strip_tags(), trim(), stripslashes() and remove quotes.
Thank you!
Look for REDIRECT_URL or REDIRECT_STATUS in the $_SERVER global. mod_rewrite should be adding these.
You can check the request uri, which is contained in the $_SERVER global variable.

Categories