Clean Urls - How Do I Accomplish This?
Solution 1:
It's usually an .htaccess thing, called mod_rewrite, if you're using website is hosted under Apache (which, using PHP, most are). A quick example of a .htaccess file would be:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$ [NC]
RewriteRule (.*) index.php?query=$1&%{QUERY_STRING}
So, in this example, http://example.com/aLink/?test=a would be redirected to http://example.com/index.php?query=aLink/&test=a.
You can then use PHP to parse the $_GET['query']
variable.
Solution 2:
Clean URLs are typically accomplished using URL rewrites, where they turn site.com?var=1&var2=2
into site.com/1/2
On the Apache server, this is accomplished using mod_rewrite. You can experiment using Wordpress, which will generate the proper Apache files and code for rewriting Wordpress stuff like ?page=name&category=that
to /name
.
Solution 3:
I think what he meant is that instead of
http://www.snoop.com/pages?id=3
you'd rather have
http://www.snoop.com/pages/snoop_is_the_best
As mentioned in the comment, depending upon the webserver, webapp setup, you could get it for free. Apache uses mod_rewrite
for e.g.
Solution 4:
With Apache and PHP, you can accomplish this without mod_rewrite as well. No one seems to care these days about the overhead needed to run mod_rewrite, but if you're interested in squeezing every bit of performance out of your web server, you might like the alternative method.
See http://www.evolt.org/Making_clean_URLs_with_Apache_and_PHP
for an explanation of the solution using Apache's ForceType directive.
Solution 5:
A clean URL is just a URL without a query string. So for example, instead of http://example.com/index.php?page=foo
you might want http://example.com/foo
They can become very complicated for large existing sites that want to move to clean URLs. As Matt H and others said, you can use Apache rewrite to change existing URLs into clean URLs. http://httpd.apache.org/docs/current/mod/mod_rewrite.html has the full documentation on mod_rewrite, but you'll probably learn faster from examples. jValdron just posted a decent one. And this answer is quickly becoming a mishmash of faster answers. There is so much documentation for rewrites and handling them with PHP that a question this broad isn't really worth answering again.
If you have a very large site with a lot of php pages that take query strings, you're going to have to plan on either writing a ton of rewrites and making sure your links don't break or a single rewrite to a script that will handle redirecting to the right pages.
If you are making a new site, I'd go with rewriting your URLs to a single script that will get the content according to the URL.
Here is a famous example from Drupal's .htaccess file.
<IfModule mod_rewrite.c>
RewriteEngine on
# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>
Then QSA
means query string append and the L
before it means Last Stop the rewriting process immediately and don't apply any more rules.
Then index.php can access the URL with $_GET['q'].
Post a Comment for "Clean Urls - How Do I Accomplish This?"