English 中文(简体)
php urls 重写规则
原标题:Rewrite rules for php urls

我试图为我心中的某个问题找到答案, 但似乎我找不到答案。

怎么可能用“?” 和“=” 来做一些php 。

例一(1):

example.com/user.php?profile=example

至:

example.com/user/profile/example

例二(2):

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

至:

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

将第二个“/” (斜线) 当作“?” 的代码, 第三个和其余作为“=” 的代码, 这样我就可以自由地使用它, 而不是为每页制作新的代码...

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)

最佳回答

添加到您的. httaccess 文件 :

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

或者更通用的版本...

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

确定 Apache s < a href=> "http://http://httpd.pache.org/docs/text/mod/mod_rewrite.html" rel=“nofollow” >mod rewriter 已启用!

问题回答

如果你可以完全进入申请程序, 你可以修改密码, 我可以给你一些小把戏,我通常用在 REST 公用设施上。

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

在您的 Web 服务器文档根( 您的应用程序路径) 中创建一个. httaccess 文件, 并将其放入 :

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

创建名为 index.php 的文件并将其放入内, 这将是你的控制器 :

<?
        // echo "REQUEST_URI: " . $_SERVER[ REQUEST_URI ] . "</br>
";
        $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>
";

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

当用户拨打""http://example.com/user/profile/ZeroXitreo" rel="nofollow">http://example.com/user/profile/ZeroXitreo 时,该页面将显示如下:

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

当用户调用http://example.com/forum/thread/example-in-an-example 时,网页将是:

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

读取控制器的 PHP 代码, 我想这是相当自我解释的。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签