Forum Moderators: phranque
I have this url:
http://site.com/member/?u=Falcon i want to change this:
http://site.com/member/?u=Falcon http://site.com/Falcon I want this modification to work on all profiles not just mine...
How can i do this just with .htaccess ?
I'm new here and if there is someone that had the same problem please post the link to that topic, will help me alot.
You cannot do it with just .htaccess, because the relevant modules available in .htaccess affect only the mapping of incoming URL requests to server filepaths, and do not 'edit' the pages of your site. The links on your pages determine what URL is seen and used by clients.
In general the steps to "changing to a friendly URL" are:
1) Edit your pages or the script which produces them, so that the links on your pages show 'friendly URLS'
2) Use mod_rewrite or other server-side mechanisms to rewrite incoming 'friendly URL' requests back to the form needed by your script.
3) Optional final step only: Externally redirect *only direct client requests* for the old 'unfriendly URL' back to the new 'friendly URL' if the new URL can be directly-derived from the old. Steps one and two must be tested and working first.
The last step requires a check that the request for the 'unfriendly' URL is coming direct from a client, and is not occurring because of the internal rewrite in step 2. If this check is not correctly implemented, then the result will be an 'infinite' rewrite/redirect loop, as the rules for step 2 and step 3 repeatedly countermand each other.
Jim
The pattern on the left of the RewriteRule must match the URL in the link that is clicked on your page, and the 'thing' on the right is the filepath to your script, plus an appended query string.
I have several suggestions, which I would term "strong suggestions," as they may cause problems if ignored:
1) Use "members" instead of "member" in your URLs to prevent the rule and the script getting confused as to whether a URL refers to the script or to a path that needs to be rewritten.
2) Do not allow *all* characters in membernames. For one thing, you must comply with RFC2396, and for another, Apache and search engines are case-sensitive. You could therefore end up with two members, one named "Bob" and the other named "bob" and the server and search engines would treat them as separate and unique unless you handle that case in your script (e.g. 301-redirect one to the other). So to avoid a bunch of extra work, just allow lowercase a-z, numbers, and hyphens only.
With all that in mind, I'd suggest:
RewriteRule ^members/([a-z][a-z0-9\-]*[a-z0-9])$ /member/?u=$1 [L]
Note that if you're on Apache 2.x or later, you could likely use AcceptPathInfo instead of a rewriterule, and retrieve the "member name" from the PathInfo variable inside your script. However, this then leaves the problem of mixed-case or invalid characters in the requested URL-path, and your script will then have to handle that.
Jim
Stept 2:
That wont happen, if someone wants to register they can use in their name just "-", "_" and space if they try something else the script will tell them that the "User name contains invalid characters" and there can't be 2 "Bob" persons with the same user name, even if someone trys "BOB", "bOb" or "bob", wont work.
Now the rewriterule:
Works, but i have some problems.
1. If someone has a user name with space (Ex: profile/Gray Falcon), tells me that:
"The requested URL was not found on this server"
-but if i register a new user name that has underline (Ex: profile/Gray_Falcon) and not space all will work.
-in a .php file i used str_replace to replace space with an underline, but it's the same...
-I think i need something in .htaccess that can change the space with an underline.
That it's all for now, that has to do with .hta
Note:
My original script uses numeric id's (Ex: member.php?u=1) but i made a change, a script that takes the user name, finds the associated numeric id then runs the original script.
This is how i got too (Ex: member/?u=Falcon) and this is why my URL's have space, because there are members that have a space in their names...
I hope this note will help you.
You are dealing with much more than just your preferences or my preferences here; Your "username" URLs must comply with the HTTP protocol specifications, and all other characters are either reserved or will have to be escaped, making handling them much more difficult. So stick with the characters and location restrictions I described/coded for best results...
Also, by limiting the characters and the positions in which they can be used, you leave the door open to offering username-subdomains (e.g. "username.example.com/") in the future. If you use a more-permissive username-character-set, then that door will be permanently closed.
Jim
To add what jd said above, do *not* ever allow spaces in usernames, and therefore always avoid spaces in URLs. Spaces have to be escaped. You do not need that hassle.
For various other reasons, underscores in user names are also a bad idea.
If you already have users with spaces or underscores, manually force a name change for those users now before you get any more.