htaccess to rewrite url?


htaccess to rewrite url?

  1. 我想让用户写一个类似的URL

    http://www.domain.com/adm/folder/page/somthing/somthing/somthing[…]

  2. 然后我想在我的PHP代码中使用URL,就像我使用这个一样:

    http://www.domain.com/adm/master.php?f=folder&p=页面&otherparam1=something&otherparm2=something&otherparm3=某件事[…]

这个URL给了我两个主要参数,文件夹和页面,以及其他一些可以变量存在与否的参数。

  1. 在我的master.php-file中,我包含了一个基于以下参数的php文件:

    $folder = $_GET['f'];
    $page = $_GET['p'];
    if(empty($page)) {
        $page = 'dashboard';
    }
    $path = $folder.'/'.$page;
    include_once 'pages/'.$path.'.php?otherparm1=somthing&otherparm2=somthing&otherparm3=somthing[...]';
    

另一个参数是给定URL的多个参数。

如何向用户显示第一个URL并在PHP应用程序中使用第二个URL?htaccess重写的东西是正确的吗

我在网上搜索过,但没能找到解决方案。

我认为你不能只通过.htaccess来完成,因为参数的数量是可变的。

加载项.htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

此规则将所有404请求传递到index.php,并在index.php中解析url

$parts = explode('/', trim($_SERVER['REQUEST_URI'], '/'));
$folder = $parts[0];
$page = $parts[1];
$otherparm1 = $parts[2];
$otherparm2 = $parts[3];
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ master.php?folder=$1&page=$2&something=$3[...] [L,QSA]
</IfModule>

然后你可以像a href一样链接页面:

<a href="http://www.domain.com/adm/folder/page/somthing/somthing/somthing[...]">Link</a>