Redirect URL entry to a specific file with a query string.

I have a website/server where I would like the following to happen.

  • If a person types the URL: http://mydomain.com/apple it should go to: http://mydomain.com/pickfruit.php?a=apple
  • And if they enter: http://mydomain.com/pear it goes to: pickfruit.php?a=pear
  • I also have the need for the ability of legit sub-directories to exist. For example, http://mydomain.com/admin would to take you into that sub-directory and would NOT redirect to pickfruit.php?a=admin.

So what I am looking for is a solution where I do not have to create all of the individual fruit directories and then put a header redirect in each directory.  

I assume this can be done with the .htaccess file or something like that? Any insight would be appreciated. I can use .php if that helps as well.
Paul KonstanskiProject SpecialistAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
You can do this with a URL rewrite in your .htaccess file.

You make it a pre-condition that the file / folder does not already exist.

It would look something like this
RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ pickfruit.php?a=$1 [L,NC,QSA]

Open in new window


The above says take any request on the domain (that does not exist as a file or folder) and pass it as a paramter to the pickfruit.php page.

I would personally give the url a sub-folder so that you can better compartmentalise this so like this
RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^pickfruit/(.*)$ pickfruit.php?a=$1 [L,NC,QSA]

Open in new window

In this example you are prefixing all your pickfruit fruit with the pickfruit/ sub folder. Just makes it neater.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Steve BinkCommented:
Just a minor suggestion for Julian's rule set:
RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*)$ pickfruit.php?a=$1 [L,NC,QSA]

Open in new window

0
Paul KonstanskiProject SpecialistAuthor Commented:
This is just what I needed. I've implemented it on this site...

If you click this URL, it redirects to the parent URL.

http://mylc.ws/easter

Thanks.
0
Julian HansenCommented:
You are welcome.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.