I've a gallery which should display different albums of pictures.
When I open the gallery with the link, I add the album name with PHP:
<a href="gallery/index.php?album=nature
Then I get the album name with $_GET['album']. It works fine.
But for Me is a clean URL important. And when I am finished with the site and upload it to a hoster, then I change the URL "layout" with the .htaccess file.
So the URL http://www.example.com/gallery/index.php?album=blacknwhite change to http://www.example.com/gallery/.
Now I think with the new URL, $_GET doesn't work anymore. Is there a alternative to $_GET to hand over the album name?
Here's again the code sample:
Site with the link:
D
index.php the PHP part:
<head>
<?PHP
$album = $_GET['album'];
?>
</head>
Your rule should look something like:
RewriteRule ^/([a-z]+)/?$ get_product_by_name.php?product_name=$1 [L]
It should not affect $_GET at all, so if it is not working it's because your rule is not setup correctly.
I would recommend you to keep relevant information in your URL, and the album-title seems relevant in this case as I assume the page is centralized around it?
Make your URL work like this instead:
http://www.example.com/gallery/<album-title>/
Since you already seem to know how to use htaccess, rewrite the URL to your "ugly" one there if you get this type of format. Your URL will look way much better then.
http://www.example.com/gallery/blacknwhite/
You need to look at something called htaccess, as you'll need to use RewriteRule which will make it pretty, but still keep $_GET['album'] available in your PHP.
Yes there is another way but its not recommended. look at http://php.net/manual/en/reserved.variables.server.php
you can use $_SERVER. but the correct way is using RewriteRule that well explained here:
http://www.webdeveloper.com/forum/showthread.php?214564-RewriteRule-with-GET-data
Related
I am trying to find a way to make pages accessible by pure URL using the $_GET[''].
But instead of the following request URL :
http://mywebsite.com/product.php?=1
I want :
http://mywebsite.com/product/1
Thank you and have a nice day.
You will need an PHP - URL-Router and mod_rewrite enabled on your Webserver.
If you're not that experienced you can use a PHP Framework, many Frameworks do have a Good Routing integrated.
Have a Look at some example tutroium: http://blogs.shephertz.com/2014/05/21/how-to-implement-url-routing-in-php/
You Should using Friendly url in php for creating spec url
you can use this approach:
how-to-create-dynamic-friendly-urls-using-php
how-to-create-friendly-url-in-php
please search friendly url in php
Create a htaccess file at root and write this code in that
RewriteEngine On
RewriteRule ^/product.php?id=([0-9]+)$ /product/$1
For more detail read about htaccess
If you are checking it at localhost than make sure you have restart your server after writing this code
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)$ index.php?$1=$2
The more number of arguments you enter here that you can access through url
$1=$2
$1=$2&$3=$4
$1=$2&$3=$4&$4=$5
Your answer is given here.
This can be achieved via url rewriting. Sounds like you want to achieve something like what a lot of PHP frameworks do.
for example http://codeigniter.com/user_guide/general/urls.html
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.
I have a data driven site that passes information to determine what the next page should show using the $_GET parameter.
I want the URL's to look nicer and be structured simply.
I have been reading about mod_rewrite but so far failed to implement it.
<?php $post = $_GET['ID']; ?>
<?php $loca = $_GET['loca']; ?>
This is taken from the URL to work out what table we want and what post ID. The URL at the moment is index.php?ID=4&loca=Pages
How would I make this work if it were instead. /pages/(the name column of the post of this ID).
This should do the internal rewrite:
RewriteEngine On
RewriteRule ^pages/(\d+)/ /index.php?ID=$1&loca=pages
It rewrites any url starting with pages/(some number)/ to the result. You should probably add some server side logic as well to do a 302 redirect if the url isn't exactly /pages/id/(Name that matches id)/. You can use $_SERVER['REQUEST_URI'] to get the string and then compare it to the string that it should be and do a redirect if it doesn't match.
Just like if you go to: https://stackoverflow.com/questions/11057691/This+is+not+the+title
You get redirected to the version with the correct title. You should also update the links you have around your site to use the new url format.
There are a lot of examples of how to do this on google.
Tutorial: http://wettone.com/code/clean-urls
Mod_rewrite: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
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)
Hey guys - im using var passing links like this to jump around a site...
<a href = "index.php?content=about.html">
...problem is, i have all the ugly var info visible in the url. I would usually hide it by using the post method, but i dont have any form tags, so is it even possible?
Thanks!!!!!!!
It's a bad idea. GET is used for reading, POST is used for updating. A better solution would be to use some sort of mod_rewrite to make friendly URLS. Often called SEO friendly URLS...
Yes, you can POST with a <a href... but you have to have a lot of ugly javascript to do it... which breaks all sort of standard conventions.
Update, combining some new information
#FDisk has the simplest solution below, but I would add a condition to it which would allow existing files to be passed through directly by the webserver without having to run it through PHP:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)$ /index.php?content=$1 [L]
That way, a request to /images/bar.png will be served directly from the filesystem if that image exists.
Note, that your page does not necessarily need to have ".html" on the end anymore. So your URL could look like: http://example.com/about which would then be converted to: index.php?content=about
Taking it one step further from the link you listed in your post, you could then parse the url for various parameter. The example you looked up stuffed them into [$_GET, $HTTP_GET_VARS, $_REQUEST] respectively, but I think that's not such a good idea. Just make your own array of parameters.
You can try using the mod_rewrite extention
The original URL:
http://www.youwebsite.com/index.php?content=about.html
The rewritten URL:
http://www.youwebsite.com/about.html
.haccess file content:
RewriteEngine On
RewriteRule ^([^/]*)$ /index.php?content=$1 [L]
you can hide info (var name and content) by encoding it. Thus the user won't be able to understand or change what you are passing around. But he will still see something in his url.
I guess you should give use some more context to understand why you cant use direct links to static pages ?