Prettify Urls with htaccess

Hello so I have a site that will show a profile:
https://code.jackzmc.me/social/u?user=Jackz
The real file is:
https://code.jackzmc.me/social/u/user.php?user=Jackz and I can if needed move /social/u/user.php to /social/user.php

but what I want is
https://code.jackzmc.me/social/u/Jackzhttps://code.jackzmc.me/social/user.php?user=Jackz in the background, so the user still sees /u/Jackz
I’ve tried using a generator as I don’t understand mod rewrite completely yet. This is the code:
RewriteEngine On RewriteRule ^u/([^_]*)$ /social/user.php?user=$1 [L] It does redirect to /social/u/?user=whatever but the ?user= is blank and it says blank even if you manually enter it in.

It’s fine if it redirects I guess, but I would like it not to. Thanks!

jz,

Due to the affect on internal links, use social/user.php.

You were close but try:

RewriteEngine on RewriteRule ^social/u/([a-zA-Z]+)$ social/user.php?user=$1 [L]

Add the R-301 flag for testing so you can see the redirection but remove it for production work.

Also, {not}_ doesn’t make much sense as there could be a LOT of garbage fed to your code. I prefer to specify exactly what I’m expecting (e.g., (Jackz|Edz|DK) ) but at least specify the allowed characters!

You might benefit from reading the mod_rewrite tutorial (http://dk.co.nz/seo) as it contains explanations and sample code. It’s helped may members and should help you, too.

Regards,

DK

I tried to change it to the thing you put. I put the .htaccess in /social directory (not root) and my name ‘Jackz’ is registered in my database and if I try it without (incorrect) the capital J it will say Not Found. Then If I put my username ‘Jackz’ with capitals it will go to /u/?user= and its blank.

I want it no matter the username to still go to /social/user.php?user= and it handle it there.

I added the ‘Jackz’ to ?user argument and it will still redirect back to no username (/social/u/?user=) with no input

Jz,

I can see that there should be a problem with internal links (social/u/Jackz is one level deeper in the file structure than social/user.php). I would restore the u subdirectory for user.php.

Now, the question was really about mod_rewrite and your description is not specific enough for me to troubleshoot. Please post your .htaccess (at least the mod_rewrite portion so I can see if there is anything else which would prevent this from working for you. Additionally, I need to see the URI you’re using, i.e., social/u/jackz or social/u/JACKZ or ???

If you will add the R=301 flag (for testing, NOT for production), you will see the redirection and be able to troubleshoot easier. E.g.,

RewriteEngine on RewriteRule ^social/u/([a-zA-Z]+)$ social/u/user.php?user=$1 [R=301,L]
OR

RewriteEngine on RewriteRule ^social/u/(Jackz|Edz|DK)$ social/u/user.php?user=$1 [R=301,L]
Just note that the second option requires that you update the mod_rewrite “member set” each time a “member” name is added, edited or deleted. (If you’re really good, you can have your add/edit/delete script modify the .htaccess file for every change to your “member set” - but that’s a more advanced topic. As it is, (Jackz|Edz:DK) is a check on your member list while ([a-zA-Z]+) will accept and pass along a lot of potential garbage for user.php to handle.)

Regards,

DK

Okay so I have a file user.php and it will handle any input of a username so if it’s not found, it will do it not apache.

Currently its case-sensitive, but the /u/ shouldn’t matter; it should no matter what go to user.php, and it will tell if its wrong or not.
so /social/u/Jackz will work /social/u/jackz will still load, but user.php will say invalid.
I put the .htaccess like this: /social/,htaccess, I’ve tried with /social/u/.htaccess and /.htaccess

Currently user.php is in /social/user.php but I can move it to /social/u/user.php

I don’t really understand mod_rewrite that much, and I don’t understand it fully so this helps a lot so far.

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