Problem with friendly username, index and many other files

Hello, I’m not sure if I’ll make any sense because this is a difficult problem to explain so bare with me. In my problem, I’m trying to achieve friendly SEO in the beginning of the URL such as http://localhost/therockers

Code below

RewriteRule ^([a-zA-Z0-9_\.-]+)$ demo/users.php?username=$1 [L,NC]

In the code above, it allows me to use alphanumeric in lowercase and uppercase, underscores, periods, and dashes. Now, the main problem that’s happening is that particular code works, but now anything from the main index to files that I strip the .php off. All of those don’t work, so now I have my default 404 error page. So if I got to let’s say http://localhost/ it says that the page doesn’t exist, but when I go to someone’s profile, it works perfectly fine. Now here comes the second problem to the factor.

In my .htaccess I have this following line.

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

This is so that I can use the index to trigger most of my codes besides the friendly usernames because that should be separate since in my PHP codes, the username won’t work if it’s triggered from the index.php file. So in order for me to actually use friendly usernames, I had to make a separate file from the index.php page.

I feel like every time I find a solution for one problem, another one begins. I already know what’s causing the problem. It’s the regex for the username. If I take off the whole period thing like

RewriteRule ^([a-zA-Z0-9_-]+)$ demo/users.php?username=$1 [L,NC]

It works perfectly fine with both friendly usernames, index, and any php file that I strip their .php extensions from the URL. The only problem that I see is that I don’t want other people taking each other’s usernames by adding a period in between a letter. So to prevent this from happening, I was going to add periods via Apache first, then in PHP, strip all of the special characters and then call every username and strip that of the special characters and compare the two strings. That way, no one can steal each other’s usernames. The period will still remain the same in the URL, it’s just for the fetching, it’ll be stripped.

So example http://localhost/the.rockers will become therockers when fetching from the database and comparing, but in the actual URL in the web address bar, it’ll still be the.rockers.

I’ve tried multiple ways, but still doesn’t work. So I’ve tried placing the rewrite rule for the username behind the index one, I’ve tried using backslash on the period for the index, I’ve tried to even backslash the file for the username. None of it is giving the result I want.

What I really want is to be able to use periods in the friendly usernames. I believe I’ve done this before because I remember asking some where. I also want to be able to use index.php to trigger most of my codes because I’m using an MVC like system. Yes, I know what you’re going to tell me. Why don’t I just use MVC to make the friendly usernames. Well, because at the same time. For the user profiles. I’m using profile.php as well, so if someone doesn’t have a friendly username, they’ll have profile.php for their profile. It gets complicated if I need to explain myself further.

Don’t you want a “not index.php” condition so you don’t end up with

And the “begins with” ^ and “ends with” $ might be a bit too restrictive, no?

Wow, I just had one of the biggest blonde moment of my life. You saved me. Looks like the ^ and $ for the index.php?url=$1 part was causing the issue. I kept thinking it was the username part, but it was actually the index part. Before when I kept trying, all the style sheets were not working. That’s why I said it would be more difficult to explain because I’m using masked names so that people actually don’t know the URL to the original style sheets and if they happen to actually access the original style sheet folder, they’ll get a 404 error since in my codes, I display that for any non-admin.

Thanks a ton. I’ll give credit where credit is do. :slight_smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.