How to make my PHP-generated URLs prettier?
-
I'd like to make more easily understood URL paths for my PHP-generated pages. Instead of using the conventional query string method such as mysite.com/page.php?section=2&product_id=1 I'd like to be able to set up my URL to read mysite.com/tools/widget/ It's more human-readable, and I think better for SEO, but I don't know how to do it! Unfortunately, all my Google and PHP.net searches around URL parsing, etc. don't seem to address how to use alternatives to the ?& method of using GET variables. Does anyone have any advice or links to articles on this subject?
-
Answer:
If you're using Apache, http://www.google.com/search?q=apache+mod_rewrite+tutorial is your friend.
fellorwaspushed at Ask.Metafilter.Com Visit the source
Other answers
You need to be searching for url rewriting. Take a look at the wikipedia page forhttp://en.wikipedia.org/wiki/Rewrite_engine. Some advice on mod_rewrite: I struggled with mod_rewrite for a couple of days to figure out get nicer looking urls. I'm not sure if my understanding of it is correct, but what seems to be happening is that mod_rewrite uses regular expressions to capture the values in a nice url and then sends a request using those values in a query string. So it was backwards from my first conception of url rewriting... mod_rewrite wasn't taking the messy query and showing me the nice url, it was taking the nice url and making it into the query string a PHP script would normally use for GET variables. So you don't have to really change what your scripts are doing so much as what your links are and adding rewrite directives to an .htaccess file to process them.
Mister Cheese
http://www.workingwith.me.uk/articles/scripting/mod_rewriteis the tutorial I used to first learn mod_rewrite, but I had to supplement it a bit with some information about regular expressions.
Mister Cheese
This http://m.alistapart.com/articles/succeed/ is what I used a few years back.
backwards guitar
You also might google around about the http://en.wikipedia.org/wiki/Front_Controller_pattern which is maybe more complicated than you want, but is the same sort of thing.
toomuchpete
Other people have beaten me to the mod_rewrite thing, but I think it should be clarified a bit: mod_rewrite is not a PHP thing. It's a webserver thing. You will NOT be using PHP to "parse URLs"... Instead, your web server (Apache, hopefully, otherwise you need a mod_rewrite alternative) will be parsing them, and converting matches to GET variables if you have it set up to do so for that particular path. Be careful with this, however. You will hamstring yourself if you use something like translating: mysite.com/*/* to mysite.com?$1=$2 ($1 just means "the first token match" or the first * you see in this particular example)... Instead, parse for SPECIFIC strings... A real-world example: I want mysite.com/foo/bar to actually go to a page in /foo/bar ... However: I want mysite.com/news to show listings of news... I want mysite.com/news/page/2 to show the second page of listings... I want mysite.com/news/entry/$entryNumber to show that entry. I want mysite.com/news/somethingfake to actually try to go to /news/somethingfake instead of screwing up... I would do something like:# This detects the section of my site someone's going to...RewriteRule ^(news|media|whatever)(/)?$ index.php?section=$1# This detects if someone's trying to look at a sub-page of my news sectionRewriteRule ^news/page/([a-z0-9]*)$ index.php?section=news&page=$1# This detects /news/entry/123 -- or with an optional SEO friendly slug, i.e. /news/entry/123/this-is-a-slugRewriteRule ^news/entry/([0-9]+)(/([\w\-]*))?$ index.php?section=news&entry=$1
twiggy
mod_rewrite has always struck me as the messy and difficult way to go about this. I think it's easier to skip the query-string stage entirely and use the http://hoohoo.ncsa.illinois.edu/cgi/env.html via the http://www.php.net/manual/en/reserved.variables.server.php variable. Here's a http://sprunge.us/LOKJ?php (apologies if weird; I haven't used php in a while). Drop that somewhere as foo.php, and then look at /foo, /foo/bar, /foo/bar/baz, etc.
hattifattener
Thanks for the tips, but unfortunately the project in question is on a shared Windows server, so it doesn't seem like .htaccess is an option, nor is compelling my client to move to Apache. Any ideas how I can implement an equivalent solution for IIS? The server uses a Plesk admin interface, if that's of any use.
fellorwaspushed
there is a URL Rewrite Module for IIS. I can't provide any more details, since I've never used IIS.
jrishel
Related Q & A:
- How To Push From Php To Android?Best solution by Stack Overflow
- How to remove namespace from generated JAXB?Best solution by Stack Overflow
- How to use external PHP file in СakePHP 2?Best solution by Stack Overflow
- How to make a dynamic table in PHP?Best solution by Stack Overflow
- How to make a php article counter?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.