.htaccess 通过 url 重写获取变量不起作用



我的.htaccess文件中有一个脚本,我正在尝试将网址从localhost/testing/user/index?username=example更改为localhost/testing/user/example

我这样做只是为了让我的网站看起来"更干净",更容易在用户周围导航。

这是我当前的htaccess文件:

RewriteEngine On
RewriteRule ^user/([A-Za-z0-9-]+)$ user/index?username=$1;
RewriteRule ^user/$ user/index;

出于某种原因,这不想为我工作,并且总是抛出 404 或 500 错误。

这是我用来获取通过URL传递的用户名的PHP代码。

if(isset($_GET['username']) && empty($_GET['username'])){
        header('location: ./');
    }
    if(empty($_GET['username']) && logged_in() == false){
        header('location: ./');
    } else if(empty($_GET['username']) && logged_in() == true || isset($_GET['username']) && $_GET['username'] === $username){
        $pageDisp = 1;
    } else if(!empty($_GET['username']) && $_GET['username'] !== $username){
        $pageDisp = 2;
}

在 PHP 中,如果 $_GET['username'] 为空,它只会将用户设置为登录用户,但如果他们已登录,它会将他们重定向到 404。

这是我的文件夹和脚本的布局:

.htaccess :/testing/user/

用户索引.php :/testing/user/

主索引.php :/测试/

这些文件的全部内容都位于我的XAMPP htdocs文件夹中。

感谢所有帮助谢谢

这段代码对我有用

匹配/testing/user/*后的任何内容

HTACCESS

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^testing/users/([^/]*)$ /testing/users/?username=$1 [L]

索引.php /testing/users/内部(测试)

<?php echo $_GET['username'] ?>

最新更新