User friendly PHP URL [duplicate] - php

This question already has answers here:
How to create friendly URL in php?
(8 answers)
SEO friendly url using php [duplicate]
(5 answers)
Closed 8 years ago.
I`ve learning development of websites recently and have appreciated quite some concepts in php and mySQL databases. Since I use GET and POST i sometimes end up with such urls: http://seta.com/news.php?articleid=231. how do i make my urls look like this instead: http://seta.com/news/today.
Maybe if one helps me on the subject so that i can search, I don`t even know what I am looking for.

This is called SEO urls or pretty urls. You can use the .htaccess file and regular expressions to rewrite the http://seta.com/news/today url to http://seta.com/news.php?articleid=231 for your program so your program can function without any modifications.
If your program in dynamically creating these links, you will have to update those places so it's outputting the urls as the new pretty urls.
Another way is to direct all urls to news.php and use $_SERVER['REQUEST_URI'] to extract the information from the url. This requires modifications to your code and it will stop working for the ?articleid=123 urls which is useful during development.

By using the GET method to submit data , a question mark followed by the submitted data will be shown like the article id for example : http://seta.com/news.php?articleid=231
<?php $article_id=$_GET['articleid']; ?>
By using the POST method , the url is not going to change :http://seta.com/news
<?php $article_id=$_POST['articleid']; ?>
i hope this is what you looking for

You need to learn some MVC php frameworks then..
CodeIgnitor is good for start
url like http://seta.com/news/today has URI Segments
where news is 1st segment and today is 2nd segment.
IN .htaccess using mode_rewrite you can remove .php from the link and create your links accordingly as you want.

Related

PHP/MySQL full file name instead of grabbing from ID [duplicate]

This question already has answers here:
using seo user friendly in php
(3 answers)
Closed 7 years ago.
I've been wondering for quite a while how do you set up php so that you instead of getting content from tables w/ get like www.mysite.com/index.php?id=1 you get them by www.mysite.com/pages/news-1.php
I have no idea how else to make up this question; but do I have to manually create new pages and put them in directories then link the page/ID via db or is there another way with mysql only.
Create a file with name .htaccess in your web root directory. And paste these lines into it (Tested):
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^pages/news-([0-9]+)/?$ index.php?id=$1 [NC,L]
URL rewriting can be one of the best and quickest ways to improve the
usability and search friendliness of your site. It can also be the
source of near-unending misery and suffering. Definitely worth playing
carefully with it - lots of testing is recommended. With great power
comes great responsibility, and all that. Aug 4, 2008 - addedbytes.com
With URL rewriting, you need to route all page requests to your "front controller" / index.php. Then you use a router to send that request to a particular controller method. In that method, you would grab the request's path (eg. /postname). Your posts database table would have a "slug" field. You then query the database for the "posts" result that contains the slug "postname".
Please check out this tutorial to learn how to make a custom framework, it'll give you the amount of background information that might shed some light on how this all works: http://symfony.com/doc/master/create_framework/index.html

Using PHP to put subpage into a Variable instead of using $_GET [duplicate]

This question already has answers here:
How to create friendly URL in php?
(8 answers)
Closed 8 years ago.
I am fully versed in how to get, and clean the vars in a $_GET and $_POST, and I understand that $_REQUEST listens for both GET and POST's.
I understand that the GET is www.somesite.com?GET=MYVAR, but my question is this. Can you take: www.somesite.com/MYVAR and put the subpage into a var, as apposed to using the ?GET=MYVAR
In PHP
Thanks,
Assuming you're using Apache, you would use the mod_rewrite module with an .htaccess file. There are tons of tutorials you can find via Google once you know the right words :)
Yes, but this is actually less dependent on PHP and more dependent on your webserver. It's called rewriting URLs. If you're using Apache, it's mod_rewrite you'll need to look into.
Here's an example for nginx using a rewrite rule:
rewrite ^([^/]+)(/)?$ /index.php?var=$1;
Then you'll be able to access it using
$_GET['var'];
Have you had a look at:
$_SERVER['REQUEST_URI']
and then split it (e.g. explode | preg_split)?
Typically this is done using the mod_rewrite as mentioned in the other answers. But you can also get the url from the $_SERVER variable and parse the resulting string.
http://us3.php.net/manual/en/reserved.variables.server.php

Passing parameters in the URL (not with php GET) [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
mod rewrite and query strings
The only way I know to pass parameters in the URL is using the GET method in PHP. But I saw many websites using what seems to be parameters directly in the URL, like this:
http://.../page/2/
In this case, is "page" really a parameter? If so, how is it handled in the code?
Or is this a regular URL of a directory "2" located in a directory "page"? Would it mean that whenever a new post is created, the website creates all the pages and the corresponding directories?
Thanks
This is called url rewriting. Basically this means that you use an apache module to rewrite incoming urls to new urls which are then handled by apache
In your example http://www.test.com/page/2/ is probably rewritten to something like http://www.test.com/?page=2.
If you search the internet for Apache URL rewrite you will get enough results explaining how you can do this.
These are all conventions. GET parameters are not specific to PHP, this is how all browsers encode form data. Java-based webapps use semicolon-separated parameters for example.
You can write a simple layer to convert ?alpha=1&beta=2 to /alpha/1/beta/2 but iw tould be just a cheap gimmick (except in a very few legitimate cases like with Squid caches).
What today's websites do is to use a single entry point pattern. Usually with apache's mod_rewrite, all requests are handled by the index.php and there is a routing facility to choose the adequate handler PHP file for a specific URL scheme. These schemes can be easily defined by Regular Expressions.
So all in all you decide how your URLs are going to look like. It is not an easy task and there are many SEO snake oil salesmen out there who will make you do all kinds of crazy stuff. What a good URL does is to identify a content (document) inside your service and you must use that single URL throughout your service to address it.
And don't forget: cool URLs don't change. You will abandon your current code base in 2 years and rebuild your site from the ground up. Design your URL scheme in a way that makes sense from a logical point of view and not something dependent on your webapp design.
The example you gave is still a GET request.
What you are looking for is URL rewriting.

php clean urls using regex

i am trying to convert these urls
localhost/list/data/?search=keyword
localhost/list/data?search=keyword
to
localhost/list/data/search/keyword
i used this expression
(.+)(\?|\/\?)([a-z0-9_-]+)=([a-z0-9_-]+)
the problem is suppose that the user searches for another keyword
localhost/list/data/search/keyword/?search=anotherkeyword
localhost/list/data/search/keyword?search=anotherkeyword
it becomes like this
localhost/list/data/search/keyword/search/anotherkeyword
how can make it like this
localhost/list/data/search/anotherkeyword
where in it replaces the previous search/keyword in the url
Its a little more complicated than just regex. The best approach is a combination of server side scripting, and htaccess. I wrote up an answer for a very similar question not to long ago. As well as use this solution in a handful of sites I develop.
PHP dynamic DB page rewrite URL
Ill save the time from rewriting the post I made on the above link and just let you use that as your guide, hope it helps

What does /#!/ mean in URL? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
What's the shebang (#!) in Facebook and new Twitter URLs for?
It usually comes straight after the domain name.
I see it all the time, like in Twitter and Facebook urls.
Is it some special sort of routing?
# is the fragment separator. Everything before it is handled by the server, and everything after it is handled by the client, usually in JavaScript (although it will advance the page to an anchor with the same name as the fragment).
after # is the hash of the location; the ! the follows is used by search engines to help index ajax content. After that can be anything, but is usually rendered to look as a path (hence the /). If you want to know more, read this.

Categories