As I know the only way to get a value from the url is to use the $_GET[]; . To do this the url must be of the format: http://domain.com/index.php?var1=value1
And then to do the following:
<?php
$value1 = $_GET['var1'];
echo($value1);
?>
What I really want is to get the value of the url in the format:
http://domain.com/value1
I know that is not a really php job, but while my index is index.php can I get the value1 from the url? Using something different rather than HTTP Get method.
Cheers.
[SOLUTION]:
I use the URL rewriting to make it work as #KevBot suggest. So what I did:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ /index.php?value=$1 [L]
It's called URL rewriting. You need to modify .htaccess.
Here's a great tutorial:
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 would like to make my urls more seo friendly and for example change this:
http://www.chillisource.co.uk/product?&cat=Grocery&q=Daves%20Gourmet&page=1&prod=B0000DID5R&prodName=Daves_Insanity_Sauce
to something nice like this:
http://www.chillisource.co.uk/product/Daves_Gourmet/Daves_Insanity_Sauce
What is the best way of going about doing this? I've had a look at doing this with the htaccess file but this seems very complicated.
Thanks in advance
Ben Paton, there is in fact a very easy way out. CMSes like Wordpress tend to use it instead of messing around with regular expressions.
The .htaccess side
First of, you use an .htacess with the content below:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Let me explain what it does (line by line):
if the apache module named mod_rewrite exists..
turn the module on
let it be known that we will rewrite anything starting after the
domain name (to only rewrite some directories, use RewriteBase
/subdir/)
if the requested path does not exist as a file...
and it doesn't even exist as a directory...
"redirect" request to the index.php file
close our module condition
The above is just a quick explanation. You don't really need it to use this.
What we did, is that we told Apache that all requests that would end up as 404s to pass them to the index.php file, where we can process the request manually.
The PHP side
On the PHP side, inside index.php, you simply have to parse the original URL. This URL is passed in the $_SERVER variable as $_SERVER['REDIRECT_URL'].
The best part, if there was no redirection, this variable is not set!
So, our code would end up like:
if ( isset( $_SERVER['REDIRECT_URL'] ) ) {
$url = explode('/', $_SERVER['REDIRECT_URL'] );
switch($url[0]){
case 'home': // eg: /home/
break;
case 'about': // eg: /about/
break;
case 'images': // eg: /images/
switch( $url[1] ){
case '2010': // eg: /images/2010/
break;
case '2011': // eg: /images/2011/
break;
}
break;
}
}
Easy Integration
I nearly forgot to mention this, but, thanks to the way it works, you can even end up not changing your existing code at all!
Less talk, more examples. Let's say your code looked like:
<?php
$page = get_page($_GET['id']);
echo '<h1>'. $page->title .'</h1>';
echo '<div>'. $page->content .'</div>';
?>
Which worked with urls like:
index.php?id=5
You can make it work with SEO URLs as well as keep it with your old system at the same time. Firstly, make sure the .htaccess contains the code I wrote in the one above.
Next, add the following code at the very start of your file:
if ( isset( $_SERVER['REDIRECT_URL'] ) ) {
$url = explode('/', $_SERVER['REDIRECT_URL'] );
$_GET['id'] = $url[0];
}
What are we doing here? Before going on two your own code, we are basically finding IDs and information from the old URL and feeding it to PHP's $_GET variable.
We are essentially fooling your code to think the request had those variables!
The only remaining hurdle to find all those pesky <a/> tags and replace their href accordingly, but that's a different story. :)
It's called a mod_rewrite, here is a tutorial:
http://www.workingwith.me.uk/articles/scripting/mod_rewrite
What about using the PATH_INFO environment variable?
$path=explode("/",getenv("PATH_INFO"));
echo($path[0]."; ".$path[1] /* ... */);
Will output
product; Daves_Gourmet; Daves_insanity_Sauce
The transition from using $_GET to using PATH_INFO environment is a good programming exercise. I think you cannot just do the task with configuration.
try some thing like this
RewriteRule ^/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+) /$1.php?id1=$2&id2=$3 [QSA]
then use $_GET to get the parameter values...
I'll have to add: in your original url, there's a 'prod' key, which seems to consist of an ID.
Make sure that, when switching to rewritten urls, you no longer solely depend upon a unique id, because that won't be visible in the url.
Now, you can use the ID to make a distinction between 2 products with the same name, but in case of rewriting urls and no longer requiring ID in the url, you need to make sure 1 product name can not be used multiple times for different products.
Likewise, I see the 'cat'-key not being present in the desired output url, same applies as described above.
Disregarding the above-described "problems", the rewrite should roughtly look like:
RewriteRule ^/product/(.*?)/(.*?)$ /product?&cat=Grocery&q=$1&page=1&prod=B0000DID5R&prodName=$2
The q & prodName will receive the underscored value, rather than %20, so also that will require some patching.
As you can see, I didn't touch the id & category, it'll be up to you to figure out how to handle that.
Please some one explain to me how it works and how to do it.
PHP has a function just for this: parse_url().
$parts = parse_url('domain.com/THESTRING');
$path = $parts['path'];
That wil get the part that you want
Could also use below if you want the current URI:
$_SERVER["REQUEST_URI"]
Yet another way:
echo strstr(str_replace('://', '', 'domain.com/THESTRING'), '/'); // THESTRING
EDIT: This should get you started.
Make sure your server support Mod_Rewrite
I opened a .htaccess in the public_html directory.
Then turned on the Mod_Rewrite by this command:
RewriteEngine on
Then I write a new law:
RewriteRule (.*)\.html$ index.php?link=$1
Thats bicycly mean that every url that ends with .html will redirect to index.php
Then in index.php I get the URL link