Forum Moderators: phranque
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.
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.
RewriteCond $1 !^profile\.php$
RewriteRule ^([^/]+)$ profile.php?username=$1 [QSA,L]
Jim
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
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]
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.