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

时间:2021-10-03 10:50:58

I have a script inside of my .htaccess file in which I am attempting to change the url from localhost/testing/user/index?username=example to localhost/testing/user/example.

我的.htaccess文件中有一个脚本,我试图将url从localhost / testing / user / index?username = example更改为localhost / testing / user / example。

I am doing this just to make my website look "cleaner" and easier to navigate around users.

我这样做只是为了让我的网站看起来“更干净”,更容易浏览用户。

This is my current htaccess file:

这是我目前的htaccess文件:

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

For some reason this doesn't want to work for me and ALWAYS throws either a 404 or 500 error.

出于某种原因,这不希望对我有用,并且总是抛出404或500错误。

This is the PHP code in which I am using to get the username passed through the URL.

这是我用来获取通过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;
}

In the PHP, if $_GET['username'] is empty, it will just set the user to the logged in user, but if they are logged in, it will redirect them to a 404.

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

This is the layout of my folders and scripts:

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

.htaccess : /testing/user/

.htaccess:/ testing / user /

user index.php : /testing/user/

用户index.php:/ testing / user /

main index.php : /testing/

主要index.php:/ testing /

ALl of these files are located inside of my XAMPP htdocs folder.

这些文件中的ALl位于我的XAMPP htdocs文件夹中。

All help is appreciated Thanks

感谢所有帮助

1 个解决方案

#1


1  

This code works for me

这段代码适合我

Match anything after the /testing/user/*

在/ testing / user / *之后匹配任何内容

HTACCESS

.htaccess的

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

index.php inside the /testing/users/ (test)

index.php里面的/ testing / users /(测试)

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

#1


1  

This code works for me

这段代码适合我

Match anything after the /testing/user/*

在/ testing / user / *之后匹配任何内容

HTACCESS

.htaccess的

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

index.php inside the /testing/users/ (test)

index.php里面的/ testing / users /(测试)

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