Forum Moderators: phranque

Message Too Old, No Replies

URL Rewrite

How to rewrite a simple URl into a name

         

agxFalcon

9:13 am on Sep 22, 2009 (gmt 0)

10+ Year Member



Hello!

I have this url:

http://site.com/member/?u=Falcon

that leads to my profile...

i want to change this:

http://site.com/member/?u=Falcon

into this:
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.

jdMorgan

1:15 pm on Sep 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> How can i do this just with .htaccess ?

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

agxFalcon

2:27 pm on Sep 22, 2009 (gmt 0)

10+ Year Member



Step 1, done

Now i need a RewriteRule tha can rewrite my:
[site.com...]
into:
[site.com...]
or just:
[site.com...]

I don't know how to do that :( a little help will be grateful

jdMorgan

5:01 pm on Sep 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No you actually need a rule that internally rewrites client requests for the URL
http://example.com/member/Falcon to the server filepath /member/?u=Falcon

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]

Here, the URL-paths in your links must start with "/members/" and membernames must start with a lowercase alphabetic character, may be followed by zero or more lowercase alphabetic, numeric, or hyphen characters, and must end with a lowercase alphabetic or numeric character. The minimum-length username is therefore two characters (You could change that to three by replacing "*" in the pattern with "+").

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

agxFalcon

2:16 pm on Sep 23, 2009 (gmt 0)

10+ Year Member



Step 1:
I used "profile" hope that isn't a problem...

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.

jdMorgan

2:35 pm on Sep 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In the code I posted above, I gave the pattern for *all* characters that you should allow as usernames -- alphabetic, numeric, and hyphens. That is all.

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

g1smd

4:03 pm on Sep 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



*** just "-", "_" and space ***

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.

agxFalcon

5:50 pm on Sep 23, 2009 (gmt 0)

10+ Year Member



I myself don't want the spaces i want to change them into a line or underline, i used str_replace to do that but it's like i changed a numeric id 1 into 2.
I doesent work and i'm sure there is a way, because i saw a lots of site that have members that use names with space, but in the URL's the space is replaced with a line.