URL re-writing in PHP - php

I am trying to implement URL rewriting in my PHP application. Can someone share a step by step procedure of implementing URL rewriting in PHP
In my application I want to implement following URL rewriting
http://example.com/fast-five
http://example.com/300
http://example.com/13-b
from
http://example.com/movie-download.php?nm=fast-five
http://example.com/movie-download.php?nm=300
http://example.com/movie-download.php?nm=13-b
One more thing which URL will be best according to SEO, management, application point-of-view out of the following two types.

Of course
http://example.com/fast-five
will be good for SEO

Are you serving your PHP through an Apache HTTP Server installation? If so:
RewriteRule ^/fast-five$ /movie-download.php?nm=fast-five [R=301]
From an SEO perspective, the first would be preferred. Using the HTTP 301 ("Moved Permanently") is most effective for this.

If your using an MVC framework like CakePHP, you should look at the documentation on routing. Otherwise, you can use the web servers rewriting rules.

Related

How to define friendly URL rules using .htaccess?

Long time I am trying to define Rewriting rules but I could not succeed yet? I have a simple page http://www.myURL.com/pdf.php?id=2. I want to make it friendly by this: http://www.myURL.com/pdf/2/ ...
I wrote this rule after spending time on google:
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^pdf/([0-9]+)\$ pdf.php?id=$1.
When I uploaded my .htaccess file on server and try to run first it give me 500 ERROR. Second time it loaded page but it could not show me friendly URL; it was showing as before.
Kindly tell me better solution, I followed many instructions to make it useful but could not successful. My Client are not going further unless I showed him friendly URL. kindly help me as soon as possible. This is my client hosting Organization: Justhost.com.
You can do this using Apache's Rewrite Engine. Something like this:
RewriteEngine On
RewriteRule ^pdf/([0-9/]+)$ /pdf.php?id=$1 [L]
This will allow you to go to http://yoursite.com/pdf/192 instead of http://yoursite.com/pdf.php?id=192
Here is a very simple tutorial on using Apache's mod_rewrite to create friendly URLs.
Probably a bit too much to take just right now but you could look at some of the available MVC frameworks to help you solve this. I'm guessing that the host only offers PHP 5.2 so I'd take a look at the routing/controller solution from Zend Framework 1.12 to solve this as that can be implemented into existing code without too much trouble.

Routing page requests with PHP the right way

I need to know the term and best practices of performing site navigation the "right"? way, similar to how stackoverflow routes you when you ask a question via the url:
"http://stackoverflow.com/questions/ask"
Where as with my knowledge of PHP programming I would probably code it like so:
"http://stackoverflow.com/index.php?p=questions&act=ask"
Hopefully you understand what I mean. I would like to know the term for this method of page navigation and request/response handling, and if possible the best practices, limitations, or anything else I need to keep in mind when designing a web application using this standard / method. I also don't even know if this is all done with PHP or some web backend coded in ASP or Ruby or what have you, so I have populated the tags with my guesses.
The pattern that most MVC frameworks use is a front controller that calls on a router. The front controller is typically an index.php in your web root. Next, all requests that aren't for existing files (like js, css, and image assets) need to be sent to this controller. In apache, you can do this with mod_rewrite:
RewriteRule ^index\.php$ - [F]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L,QSA]
However, the recommended way in Apache 2.5 is with FallbackResource:
FallbackResource index.php
IIS has similar functionality if that's what you're using.
In index.php, you can access the URL originally requested with $_SERVER['REQUEST_URI']. You should include your router (which should be outside of the web root) and invoke it with the request URI. Example:
require '../router.php';
$router = new Router();
$router->process($_SERVER['HTTP_METHOD'], $_SERVER['REQUEST_URI'], $_GET, $_POST);
Then your router can find the appropriate controller to route the request to. Read more on the MVC framework, and study some examples to better understand how others have implemented it.
They are most likely using the same method you are describing (embedding the navigation variables) within the URL, but it is being done "under the hood".
The mechanism that allows you to present URLs such as this is called MOD Rewrite. It uses the combination of the variables in the URL, and regular expressions to re-represent the URL to the end-user in a more user-friendly manner.
More Information: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
Edit: Of course this would apply to code running on apache webserver. There are probably similar modules for other web servers such as IIS.
Also, mind you that mod_rewrite is outside of the scope of php. It is instead actually apache server directives, which are invoked before php even comes into play.
I am currently developing a php router which is targeted at extreme high performance. you probably might want to take a look:
https://github.com/c9s/Pux
Pux is 48.5x faster than symfony router in static route dispatching, 31x faster in regular expression dispatching. (with pux extension installed)
Pux tries not to consume computation time to build all routes dynamically (like Symfony/Routing). Instead, Pux compiles your routes to plain PHP array for caching, the compiled routes can be loaded from cache very fast.
With Pux PHP Extension support, you may load and dispatch the routes 1.5~2x faster than pure PHP Pux.
Introducing RouteREST
https://github.com/iPhoneSDKPro/routeREST
This is my implementation of a php router. It uses MVC architecture and is designed to be simple to setup and use.
I use this in a project I developed and thought to share with the community.
At some point I would like to make it into a "Composer" library.. someday..
Enjoy
-James

Website structure / Newbie

I created a website using php, passing values from page to page either with POST or GET.
Though there is some cons, I dont know how to track specifically what data has been viewed in GoogleAnalytics as it just shows the name of the page (xxxx.php)
On the other side, I see websites structured differently with a bunch of subdirectories created : www.xxx.com/xxxxxx/xxxxx/xxx
This looks like pretty manual for me , compared to the .php?xxxx=xxxx way of structuring.
Do you know how this subdirectory style structuring can be automatically obtained?
This is done with Apache rewrite rules.
To make it so that whenever a user goes to /posts/visiting_new_york, it actually goes to to /viewpost.php?id=visiting_new_york, you create a file in your site called .htaccess like this:
RewriteEngine On
RewriteRule '^posts/([^/]+)$' viewpost.php?id=$1 [L]
Use an MVC framework like rails, or simply configure your webserver's virtual directory structure to be identical to the local servers file system and adhere to that scheme when saving your php files.
Yes, you can do this with "mod_rewrite" in apache.
It involves creating a .htaccess file with URL re-writing rules inside.
So you can transform /index.php?page=contact&lang=en into /en/contact/
Here's a good rewrite cheat sheet: http://www.addedbytes.com/cheat-sheets/mod_rewrite-cheat-sheet/
Wadih
You need to read about url rewriting
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
If you just want to track your dynamic pages , there is another solution in Google analytic
http://www.google.com/support/googleanalytics/bin/answer.py?answer=55504

mod_rewrite before of after developing application

I am trying to better understand how to incorporate mod_rewrite into a web applications development life cycle.
The question I am trying to understand is:
Do you build your application and then setup mod_rewrite after the fact?
Is it more appropriate to consider mod_rewrite along the way while you develop your application?
mod_rewrite is a substantial part of your application. If it doesn't handle just trivial requests, but for example establishes a front controller, then of course you have to have planned your application structure to know what mod_rewrite is supposed to do.
Whenever I'm building an application - first thing what I do is to redirect any /foo/bar/ or /foo/bar.html into a PHP index.php?path=foo/bar. More detailed setup ( for example for images etc. ) I do when I need it

php url-routing

I am studying php routing material now, but I still can't understand the theory. Does anyone here have good tutorials or links to share?
You are talking about url rewriting, and there are many other questions about that on here if you search for that term.
Regardless, url rewriting isn't handled by PHP, it's done by the web server. If you are running PHP under Apache, then you'll want to have a look at mod_rewrite. For IIS, you would look into the IIS Rewrite module (which conveniently can import mod_rewrite rules from apache as well)
Take a look at this: Url Routing with PHP

Categories