PHP Get method without? - php

Hey so im working on a website and one part of it allows you to lookup a user based on their name. At the moment i have it using a $_GET request so the link would look like:
http://website.com/p?name=John+Smith
How would i be able to remove that ?name= because i see alot of sites doing things like:
http://website.com/p/John+Smith
how would i achieve this because to my knowladge their arent any other forum request types only Post and Get?

URL rewriting is definitely what you're looking to do. It's well worth playing carefully with it but lots of testing is recommended. With great power comes great responsibility!
Most dynamic sites include variables in their URLs that tell the site what information to show the user. The example you provided is exactly like this.
Unfortunately, a cleaned up URL cannot be easily understood by a server without some work. When a request is made for the clean URL, the server needs to work out how to process it so that it knows what to send back to the user. URL rewriting is the technique used to "translate" a URL like the last one into something the server can understand.
To accomplish this, you need to first create a text document called ".htaccess" to contain the rules. This would be placed in the root directory of the server. To tell the server to rewrite a URL pattern, you need to add the following to the file:
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^p/[A-Za-z\+]$ /p/?name=$1 [NC,L] # Rewriting rule here
The NC bit denotes case insensitive URLs and the L indicates this is the last rule that should be applied before attempting to access the final URL.
You can do quite a bit with this one rule, but the specifics extend far beyond the space of my answer here.
https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/
I would highly suggest reading that thorough guide to help you on your quest!

Related

Create a rewrite rule in .htaccess for php file

I am very new to .htaccess, I am having some problem with file action.
Example:
http://www.domain.com/upload_pic.php?action=save_pic
I wrote the .htaccess rule like
RewriteEngine On
RewriteRule ^sabc([a-zA-Z0-9!##$-_]*)$ upload_pic.php?action=$1
I want the desired result like:
http://www.domain.com/sabc/save_pic
How can I get the desired result, please correct my .htaccess line.
Change it to:
RewriteEngine On
RewriteRule ^sabc/(.+)$ upload_pic.php?action=$1
The .+ will capture one or more characters after the / and be captured into $1
There are several other guides on the web already, but to understand it in better way as you are beginner #AMY, I have writen this for you. Hope this will work for you.
Most dynamic sites include variables in their URLs that tell the site what information to show the user. Typically, this gives URLs like the following, telling the relevant script on a site to load product number 7.
http://www.domain.com/upload_pic.php?pic_id=7
The problems with this kind of URL structure are that the URL is not at all memorable. It's difficult to read out over the phone (you'd be surprised how many people pass URLs this way). Search engines and users alike get no useful information about the content of a page from that URL. You can't tell from that URL that that page allows you to buy a Norwegian Blue Parrot (lovely plumage). It's a fairly standard URL - the sort you'd get by default from most CMSes. Compare that to this URL:
http://www.domain.com/sabc/7/
Clearly a much cleaner and shorter URL. It's much easier to remember, and vastly easier to read out. That said, it doesn't exactly tell anyone what it refers to. But we can do more:
http://www.domain.com/sabc/user-navnish/
Now we're getting somewhere. You can tell from the URL, even when it's taken out of context, what you're likely to find on that page. Search engines can split that URL into words (hyphens in URLs are treated as spaces by search engines, whereas underscores are not), and they can use that information to better determine the content of the page. It's an easy URL to remember and to pass to another person.
Unfortunately, the last URL cannot be easily understood by a server without some work on our part. When a request is made for that URL, the server needs to work out how to process that URL so that it knows what to send back to the user. URL rewriting is the technique used to "translate" a URL like the last one into something the server can understand.
To accomplish this, we need to first create a text document called ".htaccess" to contain our rules. It must be named exactly that (not ".htaccess.txt" or "rules.htaccess"). This would be placed in the root directory of the server (the same folder as "upload_pic.php" in our example). There may already be an .htaccess file there, in which case we should edit that rather than overwrite it.
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^sabc/?$ upload_pic.php [NC,L] # Handle requests for "sabc"
A couple of quick items to note - everything following a hash symbol in an .htaccess file is ignored as a comment, and I'd recommend you use comments liberally; and the "RewriteEngine" line should only be used once per .htaccess file (please note that I've not included this line from here onwards in code example).
The "RewriteRule" line is where the magic happens. The line can be broken down into 5 parts:
RewriteRule - Tells Apache that this like refers to a single
RewriteRule.
^/sabc/?$ - The "pattern". The server will check the URL of every
request to the site to see if this pattern matches. If it does, then
Apache will swap the URL of the request for the "substitution"
section that follows.
upload_pic.php - The "substitution". If the pattern
above matches the request, Apache uses this URL instead of the
requested URL.
[NC,L] - "Flags", that tell Apache how to apply the rule. In this
case, we're using two flags. "NC", tells Apache that this rule should
be case-insensitive, and "L" tells Apache not to process any more
rules if this one is used.
# Handle requests for "sabc" - Comment explaining what the rule does (optional but recommended)
The rule above is a simple method for rewriting a single URL, and is the basis for almost all URL rewriting rules.
RewriteEngine On
RewriteRule ^sabc/(.+)$ upload_pic.php?action=$1
Hope this will be helpful for you to understand URL rewriting #AMY.

Any issues with using wildcards in htaccess file?

I have the following rewrite in my htaccess file:
RewriteRule residential-(.*)-(.*)-(.*)-(.*)-(.*)-(.*)-(.*)\.html$ http://MYDOMAIN.COM/listing.php?type=rent&recordID=$7
I then use a function to build the final URL - here is an example of one possible URL:
residential-house-for-rent-sa-adelaide+hills-aldgate-895.html
The reason I have used wildcards is because there are many thousands of possible combinations which I do not want to explicitly state in the htaccess file. The only element within the URL that actually controls the final output of the page is the recordID.
My question is - are there any issues that I should be aware of with using so many wildcards in building the URL ? My concern is that ultimately a user can type anything as a URL, so long as it fits with the patterns required in the htaccess file and so long as a record ID is in position $7, and it will reach a page. I am unsure if this will have any detrimental impact on SEO or Google crawling of the site or whether there are any other potential issues that I need to think about with this structure ?
Any help is appreciated :-)
So I have looked into this and so long as you are careful not to create rewrite rules that contradict each other then I can see no issue. On a side note, I have read that it is not a great idea to have too many rewrite rules in the .htaccess file as it slows things down a little.

A Dynamic website using PHP

I am a beginner PHP programmer. I searched google for a "Dynamic PHP website tutorials". I found some stuff. They use $_GET variable to make the website dynamic, so the URL's appear like this:
example.com/?page=home
example.com/?page=about
example.com/?page=Downloads
and so on...
But most of the dynamic websites that I found on the internet has links like this:
example.com
example.com/about
example.com/download and so on....
So how do they do so ?? Have they got folders for all the catogories ?? And Also some websites have article URLs (eg : example.com/articles/posts/2010/article1.php). It would be a reall mess if they've got folders for all items. If not then How ?? Can someone give an example please ?
If you're using apache then read: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
If you're using IIS then read: http://www.iis.net/downloads/microsoft/url-rewrite
In order to use the $_GET variable, it must be in the query string (or being routed through some other means that isn't 'default').
For example, the URLs you're using would become.
example.com/?page=home
example.com/?page=about
example.com/?page=Downloads
Additionally, you can rewrite URLs using the .htaccess file (http://httpd.apache.org/docs/2.0/misc/rewriteguide.html)
You are interested in page routing.
htaccess and MVC routing may start you down the correct path :)
To echo everyone else, it's called a url rewite.
For example, the url
http://example.com/index.php?ext=blog&cat=news&date=12122012
can be rewritten as
http://example.com/blog/news/12-12-2012
This isn't automatic, it requires defining the patterns used for understanding the new URL in a file called .htaccess which usually resides in the servers root directory. Note that the preceding '.' in the filename makes it a hidden file.
When I was first getting used to PHP i found the site http://phpbuilder.com a great help. They have a lot of articles, and a forum that is fairly nice to noobies. http://devshed.com is a good site too, and has a large amount of information on subjects outside of PHP.
You can achieve that affect with folders, but most use rewrites (Apache). It's a bit too broad of a subject to go in to here, but if you just search for rewrite tutorials you'll find some pretty quickly.
The $_GET is only to get variables from the URL. While this can be used to make sites dynamic, this is a technique which is usually frowned upon.
With rewrites, you basically have a URL like /about, but the rewrite tells your server something like "act like this is actually ?page="about"), which you then use the $_GET to process.
Being PHP beginner I will not urge you to use .htaccess, As you will need to learn lot many things before you proceed further. You have 2 option to send a request one is GET and POST. You can get more information about same on internet.
Also you have an option to start your dynamic website using CMS and I will recommend you to use wordpress. CMS will have some in-built function which will help you to do your work faster. Also using their control panel you can update the URL format.
I will also urge you to go step by step and follow every tutorial that you will find on internet.
All the best
If you want to do this you have to use .htaccess file and have to load mod_rewrite in your apache server.
In you root directory create file named .htaccess
Then Write:
RewriteEngine On
RewriteRule ^(.*)\.php$ index.php?page=$1 [L,QSA]
And After that call a page
my-page.php
It will redirected as a index.php?page=my-page internally but in browser it will show as my-page.php

Need some answer about example.php/example

I am doing on a project and i stumble upon on this while going through some code. I had tried to google but could not find a correct keyword to search regarding this question.
http://www.fruit.com/example.php/red
I am confuse with this and the end of a .php it still have a character /red for example.
What is the purpose of this? Does it have something to do with POST method?
A possible explanation is that the developer is using .htaccess rewrite rules. It is most likely extra data that has to do with the page (such as you mentioned, POST and GET) and it's only done this way to make the URL look prettier or to simplify the URL.
It is also sometimes done this way to make it easier to linkback to this page, especially if the page is being generated based on the data.
A popular place to do this would be blogs. You often see blogs display links as:
http://blog.com/blog/2001/02/23/how-pretty-is-this
when in reality, the request to the server is something like:
/blog.php?year=2001&month=02&day=23&title=how-pretty-is-this
which isn't that pretty and not as easy to link-back to this particular page.
Mr.Xenotype answered your question well but missed one little part.
Does it have something to do with POST method?
No, it deals with the GET method. As he stated, it's pretty much to make the URL look "pretty", and it could actually be a crucial part of a RESTful API (bit advanced for a beginner) if that's how the site was designed.
http://www.fruit.com/example.php/red
Is the same as
http://www.fruit.com/example.php?id=red
Some tinkering with the .htaccess file is required to accomplish this using what are called regular expressions, or REGEX. If you'd like to know more I'll gladly provide some examples.
Adding a "RewriteRule" to the .htaccess file (a server configuration file) can allow the server to change the way the server files/folders are accessed
//probably what the rewrite looks like of the example you provided
RewriteRule .* example.php/$0 [L]
RewriteRules are broken into 4 blocks: action, pattern, rewrite and flag
Action: RewriteRule, essentially just lets the server know we're going to be performing a rewrite
Pattern: .* is our pattern in this example. The "." stands for any character, meaning match any character, and the "*" tells it to repeat this. So, match any character after the rewrite.
The rewrite is the page to perform this on, so example.php is the rewrite. The "$" of this tells it where to start the rewrite. So, we'll start the rewrite at the "$" symbol, and use our pattern. Our pattern ".*" tells us to match any character, in this case, red.
The [L] means last rule. This is it, no more rules to follow. You can use multiple rewrite rules for multiple pages.
So now, instead of using example.php?id=red, the server now knows that starting after "example.php/" to match any characters we supply.

best way to url rewrite a large website

i have a large database and i would like to url rewrite. it is like
www.example.com/products.php?id=111&cat=99&f=6&hjk=4545..
So I wrote url rewrite for this code to appear as
www.example.com/men/watches/casio/latest.. like that and its works fine
The problem is that I have a large database and its nearly impossible to write all url rewrites separately.
I thought of generating .htaacess file automatically using php. Does that make sense? or whats the best solution for this..?
EDIT: For #ghoti
RewriteEngine On
RewriteRule ^/([^/]*)/check/([^/]*)/SubjectId/([^/]*)/UniversityId/([^/]*)/CourseId/([^/]*)\.html$ /dsh_course_subjects_topics.php?TopicId=$1&check=$2&SubjectId=$3&UniversityId=$4&CourseId=$5 [L]
You need a single rewrite rule to catch the pattern you are looking for and pass it to a php page. For example:
(note, all of this needs to be on the same line)
RewriteRule
^/(men|women)/([a-z]+)/([a-z-])/(latest|newest|expensive|cheapest)$
/products.php?gender=$1&product=$2&brand=$3&filter=$4
In that way, you can pass generic request on to PHP where you look at $_GET to find out what the dynamic values were, and then pull up the correct items.
If you look at StackOverflow, their URLs look like this:
^/questions/([0-9]+)/([a-zA-Z0-9-]+)$
Spend some time looking at ModRewrite documentation as well as Regular Expressions. Then think how you can positively identify your product (eg, you may need to incorporate an ID in the URL nonetheless).

Categories