?
Solved

.htaccess:  Redirect like new Facebook usernames with condition

Posted on 2009-07-07
18
Medium Priority
?
391 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!
0
Comment
Question by:NeedExpertHelp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 8
18 Comments
 
LVL 43

Expert Comment

by:ravenpl
ID: 24796067
RewriteEngine On

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

Author Comment

by:NeedExpertHelp
ID: 24796096
Hi ravenpl,

Thanks for the fast response.

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

Thanks again!
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 24796138
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?
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 

Author Comment

by:NeedExpertHelp
ID: 24796160
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! :)
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 24796190
> 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 :)
0
 

Author Comment

by:NeedExpertHelp
ID: 24796216
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!
0
 

Author Comment

by:NeedExpertHelp
ID: 24796326
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.
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 24796495
Assuming it's main .htaccess (placed in the document root)

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

Author Comment

by:NeedExpertHelp
ID: 24796512
Thanks, where do I put that line?

(Yes, it's the main .htaccess)
0
 
LVL 43

Expert Comment

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

Author Comment

by:NeedExpertHelp
ID: 24796524
OK, thanks.

I'll give it a shot and report back.
0
 

Author Comment

by:NeedExpertHelp
ID: 24796558
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.
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 24796682
> 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 ...
0
 

Author Comment

by:NeedExpertHelp
ID: 24796837
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.
0
 
LVL 43

Accepted Solution

by:
ravenpl earned 2000 total points
ID: 24797033
Replace
RewriteRule (.*) /index.php?user=$1 [L,QSA]
with
RewriteRule (.+) /index.php?user=$1 [L,QSA]
to match only non-empty URIs

As for the slash problem - You right, to solve it, we may require the matching character to be non-first (. == any char)
RewriteCond %{REQUEST_URI} !.[\.-/] # to match slash also
0
 

Author Comment

by:NeedExpertHelp
ID: 24797256
That worked like a charm, thanks!

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

Just out of curiosity...
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 24797412
Yes, please read more on regular expressions on google or wherever.
0
 

Author Closing Comment

by:NeedExpertHelp
ID: 31600677
Thanks!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month14 days, 11 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question