2

I'm creating a vanity URL/ clean urls for my users. The .htaccess rewrite rules are working fine but I'm facing problem with the subdirectories.

Particularly like, when someone types example.com/user it works perfectly fine and fetch data from the page example.com/profile.php?username=user. But when somebody browse,example.com/subdirectory/user, this also shows the content of example.com/profile.php?username=user.

How can I limit the rewrite rules to only my root directory and not its subdirectory so that when someone browse example.com/subdirectory/user they will be redirected to 404 Not found as usual if there is no such file or folder in that subdirectory.

.htaccess code is as follows:

Options +FollowSymLinks RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^\.]+)/?$ profile.php?username=$1 [L] 
7
  • Sounds like you used some overly generic RewriteRule copypasta. Impossible to tell without showing that config and/or the rewrite.log Commented May 15, 2019 at 7:02
  • Check the post again, I've updated the .htaccess code... Commented May 15, 2019 at 7:10
  • add RewriteBase /subdirectory/ Commented May 15, 2019 at 7:14
  • Possible duplicate of What does RewriteBase do and how to use it? Commented May 15, 2019 at 7:15
  • So what is wrong in the code what can I change in it so that subdirectories would not rewrite url! Commented May 15, 2019 at 7:17

1 Answer 1

1

[^\.]+ is a poor placeholder, that matches almost anything (including path segments).

Since usernames are likely alphanumeric, \w+ is a better fit:

RewriteRule ^(\w+)/?$ profile.php?username=$1 [L] 

Btw, typically you'd want more speaking paths like /user/name… rather than having a magic lookup in the site root.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.