2

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.

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

This is my current htaccess file:

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.

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

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.

This is the layout of my folders and scripts:

.htaccess : /testing/user/

user index.php : /testing/user/

main index.php : /testing/

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

All help is appreciated Thanks

2
  • do you want to remove .php extension? Commented May 28, 2016 at 5:29
  • @thewbmstr nope. already done that in my root .htaccess Commented May 28, 2016 at 5:30

1 Answer 1

1

This code works for me

Match anything after the /testing/user/*

HTACCESS

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

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

<?php echo $_GET['username'] ?>
Sign up to request clarification or add additional context in comments.

1 Comment

now it is just redirecting me to localhost/testing/user/?username=example rather than just keeping it as localhost/testing/user/example :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.