Solved

Apache mod_rewrite & .htaccess - Redirect index.php/abcdefg to another page, but not index.php

Posted on 2011-02-24
2
743 Views
Last Modified: 2012-08-13
Hello Experts,

I have created a URL shortening application, similar to tinyurl.com and bit.ly. I have everything working, but now I need to determine the Apache mod_rewrite .htaccess script that will allow the following:

When a user follows a link to http://example.com/abcdefg it will display that URL, but internally will actually go to http://example.com/somepage.php where PHP will determine the "abcdefg" portion of the URL and search the database for the long URL that is associated with "abcdefg".

Note: I currently have several lines of code being used to determine what the extension (i.e. abcdefg) is. It may be a cleaner to grab the extension if I have the "internal" page be somepage.php?extension=abcdefg . Either way, it should display http://example.com/abcdefg .

I also want to be able to go to http://example.com/ without any characters (i.e. no abcdefg) following the / and have it load the default index.php page. On this page I will have the form into which you enter your long URL for shortening.

Perhaps "internally" it may need to go to a different page for the form page, while displaying http://example.com/ . I'm not sure, as I've never done any .htaccess code before.

Can you please asssist me with the .htaccess lines of code will tell Apache to do these requests? Any 'Cliff Notes" that you can add with it will be greatly appreciated as a learning tool.

Thank you!

Tom
0
Comment
Question by:PierceWeb
2 Comments
 
LVL 16

Accepted Solution

by:
sjklein42 earned 500 total points
Comment Utility
Here's a start.  This is an ".htaccess" file.

Any non-empty URL that does not contain a dot is redirected to somepage.php.  The user sees the original URL.

Any empty URL goes to index.php.

We avoid recursing by only redirecting URLs that have no dots

I have not tested it.  Sorry.

Options +FollowSymLinks
 
RewriteEngine On
RewriteBase /

RewriteRule ^[^\.]+$ somepage.php
RewriteRule ^$ index.php

Open in new window

0
 
LVL 2

Author Closing Comment

by:PierceWeb
Comment Utility
Hi sjklein42,

That works exactly as requested!

Thank you very much!

I appreciate your assistance.

Regards,

Tom
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
If you've heard about htaccess and it sounds like it does what you want, but you're not sure how it works... well, you're in the right place. Read on. Some Basics #1. It's a file and its filename is .htaccess (yes, with a dot in the front). #…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now