I have this situation:
There are many scripts that can be accessed like this:
http://website.com/script1/
http://website.com/script2/
Each script has a display.php file that receives a variable from the index.php file VIA a link. I'm using the $_GET method to reat that variable.
Example:
http://website.com/script1/display.php?id=124
What I'm trying to do is to access the display page like this:
http://website.com/script1/124
http://website.com/script2/124
Does anyone have any suggestions? My experience with htaccess is ~0.
RewriteRule script[\d]+\/[\d]+ /script$1/display.php?id=$2
This would rewrite anything like script345987/22352 to script345987/display.php?id=22352
Edit if you want to match anything, not only script, then:
RewriteRule (.*)\/[\d]+ /$1/display.php?id=$2
Related
I have a subfolder that holds user uploaded files. I want to redirect all direct file requests to this folder to another .php script...so i can check if the user is logged in before i send/show the file to him.
For example:
/mainsite/uploads/user/2324/file.pdf
needs to be forwarded to
/mainsite/uploads/permissions.php
But i need inside the permissions.php to be able to do:
$url = $_SERVER['REQUEST_URI'];
and see what was the initial request...in order to readfile() the file after all the 'checking'.
I've tried this:
RewriteEngine On
RewriteRule ^/uploads/user/?$ /uploads/permissions.php [R=301,L]
but this is just a simple forwarding...i got no idea what file or folder the user requested.
I know i can do this by creating an individual htaccess file inside every folder that is created under 'user/{userid}' but i wanted a simpler function. I dont want to have 10000 htaccess files, if i can do this with just one.
Thanks
try with this syntax: (i added the R=301 part, which wasn't necessary in my version, so it is not fully tested, works without the R option)
RewriteRule "^/uploads/user/(.+)$" "/uploads/permissions.php?file=$1" [R=301,QSA,L]
You can the get your file var in the $_GET array in permissions.php. However, i wouldn't recommend to use directly this value because it can be unsecure. The best way is to only allow fixed values, with a switch for example, having filtered the var as a string before.
How do you pass a variable to another page with a URL like the one below?
www.domain.com/results?search_query=test
Instead of like this:
www.domain.com/results/?search_query=test
(referring to the slash after "results")
I think the first URL looks nicer, but each time I try to have it that way on my site, it automatically puts a slash in.
Currently, my file path looks like so root>results>index.php (file that I pass the variable to)
Instead of making a folder called results with index.php inside it, make a php file called results.php in the root directory.
Then using .htaccesss file, you can rewrite the link to fit to what you would like.
RewriteEngine on
RewriteRule ^([^/]*)$ $1.php
If someone tries to access this page:
www.myhomepage.com/m/40921
I want the URL (above) to remain the same but the content should be from this page:
www.myhomepage.com/msg?t=40921
If it must be exactly like you said, then use this
RewriteRule ^m/([0-9]+)\/?$ msg?t=$1
But I think you made a typing error. Shouldn't it be msg.php? If so, use this:
RewriteRule ^m/([0-9]+)\/?$ msg.php?t=$1
And in your php file, you can get the t with $_GET['t'].
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.
ok assume i have php page
has this name name.php?get= and has get varible named get
ok
how i can make it appear like that name?get=
If you are using apache, mod_rewrite is one way to go. There is a whole bunch of mod_rewrite tricks here.
I'd seriously reconsider before using (or overusing) mod_rewrite.
In almost all of my projects I use a simple mod rewrite in the .htaccess:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.*$ ./
AddHandler php5-script .php
This tells the server to forward all pages to / (index.php) unless a file otherwise exists.
In the root directory I have a folder called "views" with all of the pages that I use. E.g. the file used for /home would actually be /views/home.php. However, in the index.php I have a script that parses the user's url, checks for the file, and includes that.
$page = substr($_SERVER['REQUEST_URI'], 1);
if(!$page) :
header("Location: /home");
if(file_exists("views/$page.php")) :
include "views/$page.php";
else :
include "views/$page.php";
endif;
This creates a variable called $page that stores the value of everything in the URL after the domain name. I use a substr() function on the Request URI to get rid of the trailing forward slash (/) on the URL.
If the variable is empty, for example if the user is simply at http://example.com or http://example.com/ then it forwards them to /home, where the script then checks for the home.php file inside of the views folder. If that file exists, it includes it, and displays it to the user.
Else, the script will simply include the 404 page telling the user that the file doesn't exist.
Hopefully this helps you, and if you need any further explanation I'd be happy to help!
I think you're wanting to re-write the URL client-side, which would include mod_rewrite.
In the route of your website, create a file called .htaccess and place the following code in it:
RewriteEngine on
RewriteRule ^name?get=(.*) /name.php?get=$1
Now when you type http://www.example.com/name?get=something, it will actually map to http://www.example.com/name.php?get=something transparently for you.
As far as i could understand your question, you can not strip the file extension because otherwise it will not run. In other words, you can not change:
name.php?get=
into
name?get=
But if you mean to create links with query string values that you can put them in hyperlinks in this way:
Click here !!
If you're looking to create links using a variable '$get', then you can create the link like this:
<a href="name.php?get=$get>Link</a>
Or if you want to get the value of the query string variable, you can use this:
$get = $_GET['get']