We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

.htaccess:  Redirect like new Facebook usernames with condition

Medium Priority
423 Views
Last Modified: 2012-05-07
Hi,

How can I do a Facebook-like redirect such that anything after the slash (e.g. mysite.com/[whatever] - EXCEPT if it contains a period (.) or hyphen (-) - will quietly redirect to http://mysite.com/index.php?user=[whatever] behind the scenes with the URL on the browser displaying mysite.com/[whatever] ?

Thanks!
Comment
Watch Question

Top Expert 2005

Commented:
RewriteEngine On

RewriteCond %{REQUEST_URI} ![\.-]
RewriteRule .* /index.php?user=%{REQUEST_URI} [L,QSA]

Author

Commented:
Hi ravenpl,

Thanks for the fast response.

Can you please explain what you did there and how it works?

Thanks again!
Top Expert 2005

Commented:
I don't know if it works <lol>

#if the request uri does not contain . nor -
RewriteCond %{REQUEST_URI} ![\.-]
#pass whole request uri to the index.php script (which actually contains the dot), L=last rule, QSA=query string append, user provided query string is appended to the user=[]
RewriteRule .* /index.php?user=%{REQUEST_URI} [L,QSA]

Should I go deeper with the explanation?

Author

Commented:
Thanks for the explanation.

What do you mean you don't know if it works?

I kind of need something that is sure to work! :)
Top Expert 2005

Commented:
> What do you mean you don't know if it works?
Haven't tested. My rules usually works, but from time to time first version needs patching :)

Author

Commented:
Thanks, I'll give it a try and report back.

In the meantime, if anyone else has another way of doing this (that is confirmed to work as needed), feel more than free to post it! :)

Thanks!

Author

Commented:
OK, good news, that seems to work almost perfectly.

However, it is returning the 'user' with a slash in front of it.

So mysite.com/whatever

is returning '/whatever'

How can I fix it so that it just returns 'whatever' ?

Thanks again.
Top Expert 2005

Commented:
Assuming it's main .htaccess (placed in the document root)

...
RewriteRule (.*) /index.php?user=$1 [L,QSA]

Author

Commented:
Thanks, where do I put that line?

(Yes, it's the main .htaccess)
Top Expert 2005

Commented:
Replace
RewriteRule .* /index.php?user=%{REQUEST_URI} [L,QSA]
with
RewriteRule (.*) /index.php?user=$1 [L,QSA]

Author

Commented:
OK, thanks.

I'll give it a shot and report back.

Author

Commented:
Working perfectly now, thanks!

You've already earned your points, but do you mind if I pick your brain a little bit more?

In the future, if I want to add more exceptions besides a period and hypen, what would I have to do or change?  For example, if I also wanted to ignore slashes ('/"), how would I do it?

Thanks again for all your help, I really appreciate it.
Top Expert 2005

Commented:
> RewriteCond %{REQUEST_URI} ![\.-]
The characters in [] brackets are those stop-chars (dot is special char and needs escaping)
RewriteCond %{REQUEST_URI} ![\.-/] # to match slash also
RewriteCond %{REQUEST_URI} ![\.-\+] # to match plus(special ! needs escaping) also

In general, if request uri does not matches (! == not) the following regular expression, process ...

Author

Commented:
Thanks for the info, that makes sense.

I'm having 2 problems though.

First, in the original script, if I don't enter anything after the slash (or if I do not even use the slash) as in 'mysite.com', it is still redirecting to index.php?user= and is therefore giving me an error that no user was entered.  How do I make it so that if nothing is entered after the initial slash (or if no slash is entered), it does not redirect?

Second, the RewriteCond you gave above to match slash makes the entire thing not work because I think REQUEST_URL always includes the beginning slash, so how would I do it so that it only matches a slash after the first slash (did that makes sense)?

You're been great, thanks again.
Top Expert 2005
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
That worked like a charm, thanks!

Besides symbols, is it possible to exclude/match a word (such as 'php')?

Just out of curiosity...
Top Expert 2005

Commented:
Yes, please read more on regular expressions on google or wherever.

Author

Commented:
Thanks!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.