i know that we can send variables in url by this syntax:
http://www.example.com/index.php?a=1&b=2
and i know that we have some default files like index.html,index.php,...to redirect and we can rewrite above code as below:
http://www.example.com/?a=1&b=2
but i dont understand what is this :
http://www.google.com/search?q=e
this must be :
https://www.google.com/search/?q=e
are they same ?
This:
http://www.google.com/search
would technically be a file called search
and this:
https://www.google.com/search/
a directory called search and will usually be rewritten to the index file automatically.
Read this article by Google for more info about the so called "trailing slash": http://googlewebmastercentral.blogspot.de/2010/04/to-slash-or-not-to-slash.html
someurl/?a=2 will call someurl/index.php?a=2
someurl?a=2 will call someurl
From a strict URL standpoint, no, they are not the same. Depending on the handler that processes the request, they could give you the same result, but they are different URLs.
Not necessarely. "search" can be seen as a file without ending, while "search/" can be seen as a folder, where the default page (eg. index.php) will be loaded. It also can be customized with URL rewriting.
Frankly,
http://www.example.com/?a=1&b=2 is a shortcut for http://www.example.com/index.php?a=1&b=2
While someurl in http://www.example.com/someurl?a=1&b=2 is just a resource name, similar to someurl.php or someurl.html or whatever. a dot is not obligatory for the resource name
Yes, URIs that end in directory names (not file names) are usually treated the same as if you typed a / after them. So
http://domain.tld/directory
and
http://domain.tld/directory/
are both taken to mean http://domain.tld/directory/default.file
And it's the same whether you have a ? and parameters behind it or not.
In other words, in your examples, search is the name of a directory.
Related
I have an ajax request that I'm trying to call a specific file from which is located at:
ROOT/admin/functions/upload/filename.php
And the page making the request from is located at:
ROOT/admin/customers/123
Which is modified through htaccess from
ROOT/admin/customer.php?id=123
I have tried every combination of paths I could think of but I get some strange behaviours for example when I use
../functions/upload/filename.php
It looks for the file in
ROOT/functions/uploads/filename.php
And when I use
functions/upload/filename.php
It looks for the file in
ROOT/admin/customers/functions/uploads/filename.php
So I tried
../admin/functions/upload/filename.php
And it looks in
ROOT/admin/admin/functions/upload/filename.php
I'm pulling my hair out here, has anyone got any ideas as to what this might be?
Any help would be greatly appreciated.
Thanks, James.
Since the browser knows NOTHING about your server-side paths, and it only has the path you see in the address bar, e.g.
http://example.com/ROOT/admin/customers/123
then if your ajax code looks like
$.ajax('functions/foo/bar.php');
Then the ajax call will be requesting
http://example.com/ROOT/admin/customers/123/functions/foo/bar.php
Similarly, adding ../ just strips off levels of the source page's address:
$.ajax('../../functions/foo/bar.php');
results in
http://example.com/ROOT/admin/customers/123/../../functions/foo/bar.php
^-A-^
^-------B------^
http://example.com/ROOT/admin/functions/foo.bar.php
You probably want
$.ajax('/ROOT/functions/foo/bar.php');
With that leading /, the browser ignores ALL of the subdirectory stuff in the url and uses the entire path from the ajax call as the entirety of the path.
http://example.com/ + /ROOT/functions/foo.bar.php
I'm kind of a noob at this stuff.
But I've been browsing around and I see sites that are kind alike this
www.store.com/product.php?id=123
this is really cool. but How do I do it?
Im stuck using something like this
www.store.com/product/product123.php
If you could tell me how I can go about do this it would be awesome!
What you're looking at is a $_GET argument.
In your PHP code, try writing something like this:
$value = $_GET['foo'];
Then open your page like this:
hello.php?foo=123
This will set $value to 123.
You need to use the $_GET here.
if you use the following:
?id=123
then this will be how to use it and the result
$_GET['id'] (returns the 123)
You can use as many $_GET arguments as you need, for example:
?id=123&foo=bar&type=product
$_GET is an array of what parameters are in the url, so you use it the same way as an array.
Create a file called product.php with this code:
<?php
echo "The argument you passed was: " . $_GET['id'];
?>
Now run this URL in your browser:
http://<yourdomain>/product.php?id=123
and you will understand how $_GET works.
Those are called URL parameters (what they're contained in is called a query string), and they're not unique to PHP but can be accessed in PHP using the $_GET superglobal.
Similarly, you can get POST parameters using the $_POST superglobal, though in POST requests, these parameters are not appended to the URL.
Note: Generally, for usability purposes (and thus also SEO purposes), you want to avoid using query strings as much as possible. These days, the standard practice is to use URL rewriting to display friendly URLs to the user. So your application might accept a URL like:
/products.php?id=32
But the user only sees:
/product/32
You can do this by using mod_rewrite or similar URL rewriting capabilities to turn the friendly URL into the former query string URL internally, without having the user type out the query string.
You might want to have a look at the documentation at www.php.net, especially these pages: http://www.php.net/manual/en/reserved.variables.php
Specifically, have a look at $_GET and $_POST, which are two frequently used ways to transmit information from a browser to the server. (In short, GET-parameters are specified in the URL, as in your question, while POST-parameters are "hidden from view", but can contain more data - typically the contents of forms etc, such as the textbox you posted your question in).
I'm trying to figure out what the best way to go about rewriting urls to accommodate not only a directory structure, but appended actions for a menu for a site I'm building in PHP.
I have one page that dynamically loads content based on whatever "page" is being loaded. Includes are pulled based on what the page is. Right now that's tied to directories in my includes folder. So folder/page will include includes/folder/page.php.
If I have a structure like "root/page/action/param" that works fine as long as I say "whatever is in this place in the url is this." But I ran into trouble when I started adding folders that hold different levels of pages. If I explode page out to "folder/subfolder/page" then using placement to determine what is what goes out the window. I was thinking about using something like a hyphen as a delimiter to go "root/folder-subfolder1-subfolder2-page/action/param" so I can identify the page easily but I don't really like that option.
I also don't really like tying my includes directory structure to my urls but the only other way I would think to go about avoiding that would be to have an in-memory object to handle what exactly goes where. But I want to avoid that as I want the menu to be dynamic, based on an XML file that gets parsed and urls generated accordingly. I don't want the overhead of parsing the xml and not only generating URLs but also dynamically generating an in-memory object that has seemingly arbitrary rules.
Any suggestions?
PHPonTrax uses an implementation that I think would allow for this type of routing that you want, and it's open source so you could look at the code for some inspiration. (Note: I am not making any judgement as to the framework, I just have seen the code and think it might work for you).
Basically, you store your routes in an array and you provide some placeholder's as well so that you can have multiple possible controllers/actions/ids after a given path.
So if $routes[] is our array to hold everything, we could add:
$routes[0]['path'] = "foo/bar/:controller/:action";
$routes[0]['params'] = null;
$routes[1]['path'] = "baz/:action";
$routes[1]['params'] = array("controller"=>"blah");
$routes[2]['path'] = "catalog/:id";
$routes[2]['params'] = array("controller"=>"products", "action"=>"view");
$routes[3]['path'] = ":controller/:action/:id";
$routes[3]['params'] = null;
":controller", ":action", and ":id" are unique tokens that indicate what you are allowing a token to represent in the path. So "/baz/edit", "/baz/delete", "/baz/create", "/baz/funkify", etc are all valid provided controller "blah" has edit, delete, create, and funkify methods. If the path is "/catalog/1234", then '1234' is the id and it's a valid path if and only if you can find a product with id=1234. If your request is to "/products/dothis/12345", routes 0-2 don't match, but route 3 does, so you can then look for a controller file named "products", and provided it has a method named "dothis", then you can call "dothis(12345)". Obviously, if there's no "dothis" method, or '12345' can't be found, it's an invalid path and you can raise an exception. Note you need to provide a default route, and in this case it's $routes[3].
In your file structure, you can have:
/app
/controllers
/views
/etc, etc
Possible drawback is that in your rewrite, you send every request to one php script.
If this interests you: PHPonTrax. You will want to look at the router class and action_controller class (see the process_route() and recognize_route() methods).
Use Absolute Paths instead of relative in your menu html & scripts and you shouldn't have problems as it will always point to correct source.
Absolute Path Example (notice the /):
Home
Services
My Page
My Page 2
I want to create a link like the following:
http://www.myurl.com/?IDHERE
What i want to be able to do is be able to goto the above link, and then pull whats after the ? (in this case IDHERE) and be able to use that information to perform a MySQL lookup and return a page.
Can anyone point me into the right direction? please know this is using PHP not ASP
The issue here is not with your scripting language, but with your web server setup. I'll refer to these by their Apache names, but the features should be available in most web servers.
There are three features you might want to use:
1) content negotiation (mod_negotiation), which allows your web server to try a specified list of extensions in a specified order, for example: http://example.com/foo might be http://www.example.com/foo.html or http://example.com/foo.php
2) DirectoryIndex, which tells the web server that when a client asks for http://example.com it should look for a specified list of files in order, so it might server up http://example.com/index.html or http:/example.com/index.php
3) mod_rewrite, which allows you to basically rewrite the URL format received by the server. This allows you to do things like translate http://example.com/foo/bar/baz to http://example.com/foo/bar.php?page=baz
The rest is done by the backend script code as normal.
Create a default PHP file in that directory that will get loaded when no file name is specified (e.g. index.php). In your PHP script you can get the part after the question mark from the variable $_SERVER['QUERY_STRING'].
Do the following in your site's main index.php:
list($id) = array_keys($_GET);
// right now, $id represents the ID you're looking for.
// Do whatever you want with it.
In the link, form or whatever - index.php?id=someid
In your index.php file:
$_GET['id'] = $id
Now you can use it:
e.g.
echo $id;
Since it's your default page, it will work without the extension.
list($id) = array_keys($_GET);
// right now, $id represents the ID you're looking for.
// Do whatever you want with it.
this was exactly what i was looking for, though now i just need to create something to notify if nothing is there or not. Thank you all for your responses.
I would solve it by using .htaccess file if possible.
create a .htaccess file in the main directory with the content:
RewriteEngine on
RewriteRule cat/(.*)/(.*)/(.*)/$ /$1/$2.php?$3
that should translate "example.com/foo/bar/baz" to "example.com/foo/bar.php?page=baz"
I built an client website with php script purchased recently and the support for script is pathetic, I want the permalinks of the script to be search engine friendly but they're not. They have spaces inbetween which are not at all indexed by search engine..
So, how can I change the permalinks? Thank you all..
If you've got 50 PHP files and an .htaccess file that come with this "script", you'll likely first have to find the programming path that flows through them. If you take a look at the .htaccess file, you should see some ModRewrite lines, which should end with a PHP file name. That's the script that's receiving (and decoding) the permalinks. That file would be a good place to start looking for a hook to rewrite the permalink structure. If you could post the source code (or put it somewhere like pastebin and post the link) for that file, I'd be glad to take a look.
From one of your other comments it sounds like at least part of the script is using the Smarty PHP template engine. If so, if you can find a folder that contains "cache", "templates", and "templates_c" folders (or similar), you can rule that one out as well; that would be the templates used to show the page, and not any of the decode/encode scripts.
EDIT: Looking at your .htaccess file, line 29 looks to the be one that deals with article permalinks, and it points to view.php, and converts what was the permalink into id and title GET variables. Post the source of view.php if you could, and we'll go from there.
EDIT 2 Okay, looking at view.php gained a little more insight. Primarily of which is that there is no decoding function; the Answerscript engine promptly discards the 'title' part of the permalink and only looks up a question by its ID (number following the pipe on the URL query; you can prove this on their demo page by changing the title part of the URL to anything else, and it will still fetch the right page). So, the good news is that there's no decoding function that needs to be updated when you change the encoding function. Unfortunately this does very little to tell us where the encoding function is in the scripts.
The only hint is that the view.php file includes a file called include/functions/import.php, which I'm presuming has function definitions for does_post_exist($PID), update_last_viewed($PID), update_your_viewed($USERID), and update_viewcount_question($PID). Let's see the source of that file to see if there's any other functions in there that would be used for importing. Also, how many files are in the include/functions/ folder? If there's only a few, post all their sources; likely the encoding function is defined in there. If there's a bunch, is there an export.php file in that folder (i.e. the opposite of import.php that was used by view.php)? Post that file's source as it likely has the encoding function.
EDIT 3 There they are: in the main.php file there's a trio of functions: seo_clean_titles, insert_seo_clean_titles, and seo_clean_titles2. insert_seo_clean_titles is a function to be called from within a Smarty template (search all files that have a .tpl extension for {insert name="seo_clean_titles" to see where that's used), and the difference between seo_clean_titles and seo_clean_titles2 is that the first echoes out the result, while the second returns it. However, all three have the line $title = str_replace(" ", "-", $title);, which should be turning all spaces in the title to hyphens. If you're not seeing that result, likely the code is not calling these functions at the right places. You can search through all the .php files and see if anywhere else there's a call to seo_clean_titles or seo_clean_titles2, and make sure the result is being actually used as the final URL.
Edit 4 To add ".html" to the end of all URLs: Here's the line in the template file linking to the question page:
{$ques[i].title|stripslashes}
Change that to:
{$ques[i].title|stripslashes}
and the links will have ".html" on the end. You'll then need to modify view.php to strip the ".html" back off again when parsing out the ID number: Right before $pid = intval($ph);, insert the following:
if (strtolower(substr($pid, -5)) == ".html") $pid = substr($pid,0,-5); // Remove ".html" if it exists
That should do it!
The urlencode function would take care of the spaces in the URL.
For example:
<?php
$base_url = 'http://example.com';
$category = urlencode('some thing');
$item = urlencode('Name of an item');
echo "<a href=\"{$base_url}/{$category}/{$item}/>{$item}</a>";
?>
This would make the link http://example.com/some+thing/Name+of+an+item/ which a search engine should be fine with.
And, assuming you are using URL rewriting (like mod_rewrite), the values should reach your PHP script as they were before the urlencode function. If not, you can revert them back with urldecode.