user1419532
user1419532

Reputation:

Rewrite rules for php urls

I've tried to find an answer for a question i had in my mind, but it seems to I can't find it.

How is it possible to make some php urls with "?" and "=" to /

Example one (1):

example.com/user.php?profile=example

to:

example.com/user/profile/example

Example two (2):

example.com/forum.php?thread=example-in-an-example

to:

example.com/forum/thread/example-in-an-example

Like a code that takes the second "/" (slash) as a "?" and the third and the rest as a "=" so i can freely use it instead of making a new one for each page...

LIKE: /forum (or any others) is like the page itself AND: /thread (or any others) is like the $_GET AND: /example-in-an-example (or any others) is like the value to the $_GET

EXTRA: here is a code from Jeroen:

RewriteRule ^(.*)/(.*)/(.*)/$ $1.php?$2=$3 [L]

Problem one (1): when going to like: "example.com/forum" or "example.com/user" its giving a 404 error Problem two (2): When using links like "example.com/forum/thread/test-thread/reply/2" it gives 404 error, (supose to loop with "&" and "=" after making the 1st real one so its enable to use more than one $_GET)

Upvotes: 0

Views: 175

Answers (2)

dAm2K
dAm2K

Reputation: 10349

If you have full access to the application and you can modify the code, I can submit you some trick I usually use for my REST utilities.

Put AllowOverride All inside your apache configuration to enable .htaccess file. Assure to LoadModule your mod_rewrite module too.

Create a .htaccess file into your web server document root (your application path) and put this stuff inside:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

Create a file called index.php and put this inside, this will be your controller:

<?
        // echo "REQUEST_URI: " . $_SERVER['REQUEST_URI'] . "</br>\n";
        $controller = explode("/", $_SERVER['REQUEST_URI']);
        //print_r($controller);

        $resource = $controller[1];
        $operation = $controller[2];
        $operation_value = $controller[3];

        echo "Requested resource: $resource, opetation: $operation, value: $operation_value<br>\n";

        switch($resource) {
        case 'user':
                echo "User requested\n";
                //require_once("user.php");
                break;
        case 'forum':
                echo "Forum requested\n";
                //require_once("forum.php");
                break;
        /* add any other resource */
        default:
                echo "Requested page was not found.\n";
                break;
        }
?>

When the user call http://example.com/user/profile/ZeroXitreo the page will be renderer as:

Requested resource: user, opetation: profile, value: ZeroXitreo
User requested 

When the user call http://example.com/forum/thread/example-in-an-example the page will be:

Requested resource: forum, opetation: thread, value: example-in-an-example
Forum requested 

Read the PHP code of the controller, I think it's quite self explaining.

Upvotes: 1

Jeroen
Jeroen

Reputation: 13257

Add this to your .htaccess file:

RewriteEngine On
RewriteRule ^user/profile/(.*)/$ user.php?profile=$1 [L]
RewriteRule ^forum/thread/(.*)/$ /forum.php?thread=$1 [L]

Or a more generic version...

RewriteEngine On
RewriteRule ^(.*)/(.*)/(.*)/$ $1.php?$2=$3 [L]

Make sure Apache's mod rewrite is enabled!

Upvotes: 3

Related Questions