Forum Moderators: phranque

Message Too Old, No Replies

Username Redirect

         

melissal

10:08 pm on Dec 30, 2009 (gmt 0)

10+ Year Member



I have a database with users.

I want to be able to go to http://example.com/melissa and have that redirect to http://example.com/profile.php?username=melissa where melissa is pulled from the database.

How do I do this?

melissal

11:20 pm on Dec 30, 2009 (gmt 0)

10+ Year Member



I've tried a few things and this is the latest...

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

But it's going to a 404 page.

g1smd

8:11 am on Dec 31, 2009 (gmt 0)

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



There's another problem there. Are you sure you want all requests, even for robots.txt, images, CSS files, etc, to also be handled the same way?

Make the pattern more specific, perhaps based on a URL like

/user/[i]<username>[/i]
instead.

Additionally, in your question you asked for a redirect, but your code is for an internal rewrite. A rewrite is what you require. Be careful with the terminology. They are two different things.

melissal

7:14 pm on Dec 31, 2009 (gmt 0)

10+ Year Member



Yes, i'm looking for a rewrite.

I have other rewrites that re-route all other files -> ie. http://example.com/faq goes to http://example.com/faq.php so that the extension is hidden.

Is the code that I have correct? It sends me to a 404 page. Is there a way to exclude certain folders, ie. image, styles, js, etc?

I'd prefer to keep it so that it's http://example.com/<username> ...Like how Twitter operates.

jdMorgan

10:14 pm on Dec 31, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



At the very least, you need to stop the infinite recursion:

RewriteCond $1 !^profile\.php$
RewriteRule ^([^/]+)$ profile.php?username=$1 [QSA,L]

Otherwise, you'll both over-load your server with the recursive requests, and you'll find that all requests end up as "/profile.php?username=<username>&username=profile.php&username=profile.php&...&username=profile.php" or similarly-"deformed."

Jim

melissal

8:05 pm on Jan 1, 2010 (gmt 0)

10+ Year Member



The rewrite rule works when I go to http://example.com/profile/<username> but I get a 404 page when I try http://example.com/<username>

How do I get it to work from http://example.com/<username>?

Thanks!

jdMorgan

8:25 pm on Jan 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Put the code in example.com/.htaccess, instead of in example.com/profile/.htaccess

I should warn you that you're likely to have trouble with this approach unless you're quite sure that you've got all other URLs handled before this new rule executes. Be especially careful of URLs for "known-location files" such as robots.txt, sitemap.xml, and favicon.ico.

The safest and most efficient way to handle this kind of thing is to prefix all 'user' directory URL-paths with a subdirectory path (such as you've already got with "/profiles"). You can shorten the URL-path name to "/user" or even just "/u" if you like, but having the "user" URL-space partitioned off from your "main site's" URL-space is an efficient way to prevent "URL-space collisions" between usernames and other URLs (such as "faq" in your example.) It will also prevent Mr. Frank Anderson Quante from being disappointed that he can't sign up for username "/faq" on your site. Notice that I'm talking about URL-space here; You can put the files anywhere you like, as you've already seen with your .htaccess code experience so far.

Jim

melissal

7:35 am on Jan 2, 2010 (gmt 0)

10+ Year Member



Hi Jim,

Thanks for your help. I never had a profile folder so it's weird that it was acting like that. I've deleted and re-uploaded my files and it's working how I wanted except that every url is going to the profile.php page, ie. http://example.com/faq is showing the profile.php page.

Here's my file...Am I doing something wrong?

AddHandler php5-script .php

RewriteEngine on

RewriteRule ^faq$ faq.php [NC,L] # Handle requests for "faq"
---->OTHER RewriteRules HERE<----

RewriteCond $1 !^profile\.php*$
RewriteRule ^([^/]+)$ profile.php?username=$1 [QSA,L]

melissal

4:50 pm on Jan 3, 2010 (gmt 0)

10+ Year Member



Got it working...Thanks for the help along the way!

AddHandler php5-script .php
RewriteEngine on
RewriteRule ^faq$ faq.php [NC,L] # Handle requests for "faq"
RewriteCond $1 !^profile.php*$
RewriteRule ^([A-Za-z0-9-_]+)/?$ profile.php?username=$1 [QSA,L]

g1smd

6:37 pm on Jan 3, 2010 (gmt 0)

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



Do yourself a big favour with the code layout.

Don't put comments on the same line as code.

Add a blank line after each RewriteRule line.

Precede each chunk of code with a comment describing what it does.

Beware of

!^profile.php*$
pattern. The * in this pattern means it will also match a profile.phppppppppp URL request. Do also escape the period as \. too.

The A-Za-z pattern can be replaced by a-z if you add the [NC] flag. That's more efficient to process.

By allowing a URL to deliver content both with and without a trailing slash you have created a Duplicate Content problem. You need to add a preceding rule that redirects requests with a slash so that a new URL without the slash is requested. Then and only then deliver the content.

Additionally, you need a non-www to www canonicalisation rule after any specific redirects, and before any of the rewrites.

jdMorgan

8:43 pm on Jan 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Comments on the same line as code cause a "warning" which calls the server error-logging function, even if warnings are not set to be logged. This slows down the server.

I concur with all the other points as well..

Jim