PHP和Apache路由URL路径路由、路径、PHP、Apache

2023-09-02 20:34:20 作者:时间的尘埃

我想创建一个漂亮的URL结构为我的网站。

我的路由器类只会工作,如果URL中的风格?东西=价值。

我如何得到它,所以它会像:

  /事/值
 

在我的.htaccess我有:

 有FollowSymLinks
RewriteEngine叙述上

的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则(JS | TXT | GIF | JPG | PNG)!?$ index.php文件$ 1 [L,QSA]
 

和我的路由器类我做:

 类的init {

    功能__construct()
    {

    $ URL = SUBSTR($ _ SERVER ['REQUEST_URI'],19);
    $ URLElements =爆炸('/',$网址); //如果需要进行调整。

    $类= $ URLElements [0];
    $方法= $ URLElements [1];

    如果(($吨= substr_count($网址,'/'))大于1)
    {
        为($ i = 2; $ I< $ T + 1; $ I ++){
            回声$ URLElements [$ i]。'< BR />';
        }
    }
    }
 
win环境20分钟搭建php sql服务器Apache php mysql在windows下的安装与

}

感谢Jason,我的.htaccess现在只是:

  FallbackResource /t2013/public_html/index.php
 

解决方案

对于一个快速的方法来处理正投端控制器的与Apache,使用 FallbackResource ,并用PHP解析URL。

  FallbackResource的index.php
 

I am trying to create a nice url structure for my site.

My router class will only work if the url is in the style of ?something=value.

How do I get it so it will work like:

/something/value

In my .htaccess I have:

Options FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !.(js|txt|gif|jpg|png)$ index.php?$1 [L,QSA]

And in my router class I'm making:

class init {

    function __construct()
    {

    $URL = substr($_SERVER['REQUEST_URI'], 19) ;
    $URLElements = explode('/', $URL) ; // Adjust if needed.

    $class = $URLElements[0] ;
    $method = $URLElements[1] ;

    if(($t = substr_count($URL, '/')) > 1)
    {
        for($i=2;$i<$t+1;$i++) {
            echo $URLElements[$i].'<br />';
        }
    }
    }

}

Thanks to Jason, my .htaccess is now just:

FallbackResource /t2013/public_html/index.php

解决方案

For a quick way to handle Front-end Controllers with Apache, use FallbackResource and parse the URL with PHP.

FallbackResource /index.php