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?
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.