Replace articles.php?id=123 with /articles/123-title-of-article - php

I am building a php+mysql site which will have numerous articles. I am pretty ok with html php jquery etc. I need to now what are the steps I need to take in order not have http://www.mysite.com/articles.php?id=123 but to have http://www.mysite.com/articles/123-title-of-article?
Thanks

Well, you need, for instance, to store the token for each article (to optimize), like your example "123-title-of-article". Next step is to use .htaccess and mod_rewrite from Apache (if is your case).
If you will do it only on this articles "page folder" (articles/), I suggest to you make this folder valid and make the .htaccess inside that and redirect to articles.php?id={POST_ID_FOUND_BY_TOKEN}.
Take a look too on $_SERVER["REQUEST_*"] variables from PHP.
See: this page, on end of article have some examples.

The usual way to do this is by using mod_rewrite.
You create a .htaccess file which, behind the scene, redirects the latter request to the former.
You can read about it here: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html

You'll need something called mod_rewrite, which is an apache module.
The configuration looks like this (stolen from a client's drupal install):
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule articles/(.*)$ articles.php?id=$1 [L,QSA]
</IfModule>
I haven't tested this, but it should work.

Related

Treat directory as request for a PHP file

This question is related to PHP
How do I make a request to a directory on my server (that doesn't exist) become treated as a variable.. For example:
domain.com/username will really be a request to domain.com/profile.php?user=username
Is this even possible? Or how does YouTube/Twitter/Facebook do it?
Jeff, this is called "friendly URL" and is done with url rewrite. I recommend reading this documentation: http://httpd.apache.org/docs/current/mod/mod_rewrite.html.
If you are not familiar with regular expressions you shoud read http://www.php.net/manual/en/reference.pcre.pattern.syntax.php
If you using apache as web server create a .htaccess file on your directory with following content to achieve this
RewriteEngine On
RewriteRule ^[a-zA-Z0-9_.-]+$ profile.php?user=$1 [L]
Your looking for mod rewrite. Heres a short tutorial
Most PHP frameworks have libraries that make this much easier, your best bet is probably to use one of them.

Domain/URL Masking

I have a website that passes some GET variables to different pages in PHP. My issue is that now I have a url with variables i.e. index.php?category=categoryname and that's not very memorable.
Is there any way I can change the URL to something like /categoryname instead without duplicating the page and storing in folders? But also allow users to type in /categoryname and be redirected to the correct page?
.htaccess Apache mod_rewrite, almost every professional dynamic website uses this method (like stackoverflow).
The method is fully explained in this article far better then I could ever explain it in this answer box.
You should look into writing some apache Mod_Rewrite rules in a .htaccess file.
The solution is discussed here:
this is done by the rewrite module of apache and this handles regular
expressions. You have to put a rule
like this in your .htaccess file on
the root of your website:
RewriteRule ^cat/([0-9]+)$
/index.php?category=$1
^ means the start of the url after
www.example.com/ $ means the end of
the page.
www.example.com/cat/123
will be converted by the server to:
www.example.com/index.php?category=123
In PHP you use the normal $_GET['id']
variable. The rewrite module must be
enabled by apache... This is mostly
used to make the url format
independent of the serverside
scripting language so the .php in the
url is not logical. Thats why i
changed it to product/ . The .htaccess
starts with
RewriteEngine On Options
+FollowSymLinks RewriteBase / Here all the rewrite rules.. ...

Is it possible to associate /upload/picture to /upload.php?what=picture with htaccess?

Is it possible to associate /upload/picture to /upload.php?what=picture with htaccess and when I submit a form to /upload/picture it is taken by the upload.php? If it is, how?
The question is slightly awkwardly worded, but if I understand you correctly, what you're looking for is called mod_rewrite.
mod_rewrite is a plug-in for the Apache web server which allows you to change your old-style query URLs into other formats. You will need to have mod_rewrite installed to be able to use it, obviously. Most Apache installs these days do include it by default, though.
mod_rewrite commands are entered into your htaccess file. Something like this should do it for you (but note that I haven't tested this! you may need to tweak it!):
RewriteEngine on
RewriteRule ^upload/([^/\.]+)/?$ upload.php?what=$1 [L]
Give this a go :)
RedirectMatch 301 /upload/(.*)$ http://www.yoursite.com/upload.php?what=$1
I think that should do it.

how to change the way of GET variables work in php

I dont like something like this: example.com/?id=2222. How to change it into example.com/2222?
You need to look into mod_rewrite.
From the documentation: This module uses a rule-based rewriting engine (based on a regular-expression parser) to rewrite requested URLs on the fly.
So the url can look like example.com/2222 for the user, but then translated into example.com/?id=2222 on the server.
Make sure mod_rewrite is enabled, and you'll need a few lines in an .htaccess file, similar to:
RewriteEngine on
RewriteBase /
RewriteRule (.*)$ index.php?id=$1 [L]
You can get more information (with lots of examples) here, or by searching for "htaccess pretty urls" in a search engine.
If you are unable to use mod_rewrite (which is the best way), you could use a 404 trap.
That is, normally example.com/2222 would show a 404 page to the user. Well if you set your server to point to a script of your choice instead of the default 404 page, you can now take the URL example.com/2222 and redirect the user to wherever you want.

Can I do use a direct URL like twitter or myspace in PHP

Can I do that thing that twitter and many other sites do where a url shows a users page.
www.mysite.com/the_user_name
In php... or how does one do it?
Look into how to use the mod_rewrite module in .htaccess.
Here's some ALA goodness:
How to Succeed With URLs
URLS! URLS! URLS!
The easiest method would be to use mod_rewrite in Apache (as opposed to doing it in PHP). Here's a good beginner's guide.
The easiest way that I know of is to create a .htaccess file for the website with the RewriteEngine turned on.
For example
RewriteEngine On
RewriteRule ^(.+) /index.php?user=$1
Do a google search for .htaccess and RewriteEngine to get a better grasp of the process or creating an .htaccess file.

Categories