mod_rewrite in htaccess help needed

Hi,

I've basically written my own shortlink php script.

The urls come in like this: http://short.com/8359

I have this in my .htaccess file:

RewriteEngine on
RewriteRule ^([0-9]*)$ index.php?id=$1 [L]

which gives me the number as $id and points everything to the index.php. I take it from there and process it in my php script to get the user to the right location.

All works great..... except....

I have some old shortlink urls in the wild that are like this:

http://short.com/?p=8359

and I need to account for those and get them to the right place.

How can I change the .htaccess file so that if an old style url is used, I can take that number and use it as the value of $id for use in index.php?

Does that make sense?

Thanks for any help,   Chris
St_Aug_Beach_BumAsked:
Who is Participating?
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.

arober11Commented:
Simple enough:

RewriteEngine on
RewriteRule ^([0-9]*)$ index.php?id=$1 [L]

RewriteCond %{QUERY_STRING}  ^p=([]*) [NC]
RewriteRule ^$ /%1 [L,R=301]

Open in new window

0
Ray PaseurCommented:
If your rewrite rule works correctly, but the old short links arrive in the index.php with the p= argument intact, you have a really easy PHP solution.  In the PHP script, you can do something like this:
if (!empty($_GET['p]))
{
    $_GET['id'] = $_GET['p'];
}
/* REST OF PROCESSING USING id ARGUMENT */

Open in new window

0
Ray PaseurCommented:
One other idea... Just use p= instead of id=
RewriteEngine on
RewriteRule ^([0-9]*)$ index.php?p=$1 [L]

Open in new window

Best regards, ~Ray
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

St_Aug_Beach_BumAuthor Commented:
Arober11 - I get an internal server error trying this:

---------------
RewriteEngine on
RewriteRule ^([0-9]*)$ index.php?id=$1 [L]

RewriteCond %{QUERY_STRING}  ^p=([]*) [NC]
RewriteRule ^$ /%1 [L,R=301]
---------------

Ray - $p does not seem to be passed to index.php.... I get:

if I just try:

--------------------------
$p = $_GET['id'];
echo $p;
------------------------

Notice: Undefined index: p in /home/n4s/public_html/index.php on line 2

Should it be being pass through?
0
Ray PaseurCommented:
My comment was based on the understanding that this worked...
RewriteEngine on
RewriteRule ^([0-9]*)$ index.php?id=$1 [L]

Open in new window

...and that is was assigning the value at the end of the URL to the $_GET['id'].  So I was thinking, "Why not just assign it to $_GET['p]?"

Looking at this:
--------------------------
$p = $_GET['id'];
echo $p;
------------------------

I do not see any way that you could get this message:
Notice: Undefined index: p in /home/n4s/public_html/index.php on line 2

The only index in the example is 'id' so the undefined index: p does not make any sense.  Are you sure you're testing the right script?
0
St_Aug_Beach_BumAuthor Commented:
I'm sorry Ray, my mistake, that should have been:

---------------------------------
$p = $_GET['p'];   //p
echo $p;
---------------------------------

RewriteRule ^([0-9]*)$ index.php?id=$1 [L]

Does work in that it passes through the numbers in this url:

http://short.com/8359

as $id in my script, that works fine.

However, if the url is in the style I used to use:

http://short.com/?p=8359

The numbers are not available to index.php (either as $p or as $id).

Does that make sense?
0
Ray PaseurCommented:
Yes, I think it makes sense.  Can you please test this combination and let me know what happens?

http://short.com/?p=8359
RewriteRule ^([0-9]*)$ index.php?p=$1 [L]
var_dump($_GET);
0
St_Aug_Beach_BumAuthor Commented:
With http://short.com/?p=8359         //the old style I need compatibility with

array(1) { ["p"]=> string(0) "" }

----------

the same setup just changing to the new style url I'm using: http://short.com/8359

array(1) { ["p"]=> string(4) "8359" }
0
St_Aug_Beach_BumAuthor Commented:
I don't understand how the regex works - I don't think this makes a difference in the ?=p case because it doesn't in the other, but the short domain name does have a number in it, like this: m4p.us/   Could that be messing with us?
0
Ray PaseurCommented:
Yeah, I see the problem now.  The regex is saying this:
^  AT THE START
(  CAPTURE GROUP
[0-9]  NUMERIC DIGITS
*  ANY NUMBER OF DIGITS
)  END CAPTURE GROUP
$  AT THE END

So numbers in the URL are getting picked up.

Here's my suggestion... Delete this question and repost it.  Some experts will not come to questions that already have a working thread because they assume (usually correctly) that the issues are getting handled.

Maybe ARober11 can return to the new question and try to help.  Sorry I couldn't figure this one out. ~Ray
0
David S.Commented:
The code posted by ARober11 seems to contain an error. The "[]" should have been "[0-9]".

I second Ray's suggestion with one modification. Use the "+" (instead of the "*") to match a numeral at least once, to make sure that the rewrite rule won't overwrite the value for "p" in the query string.
RewriteEngine on
RewriteRule ^([0-9]+)$ index.php?p=$1 [L]

Open in new window

[...] the short domain name does have a number in it, like this: m4p.us/   Could that be messing with us?
No, since we're not explicitly using a RewriteCond to check the domain name, the domain name is irrelevant.
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
St_Aug_Beach_BumAuthor Commented:
Hi all:

Thanks Kravimir -

RewriteRule ^([0-9]+)$ index.php?p=$1 [L

works and I'm just passing on $p either way to the script and going with that.

Oh boy, now how do I award points on this one  :)
0
St_Aug_Beach_BumAuthor Commented:
Ok.......

arober11 came up with this:  RewriteRule ^([0-9]*)$ index.php?id=$1 [L]

and Ray adapted it into this:  RewriteRule ^([0-9]*)$ index.php?p=$1 [L]

 and Kravimir improved on that with this: RewriteRule ^([0-9]+)$ index.php?p=$1 [L]

So I'll go with a three way split close as I can - hope that feels fair to everyone.

Thanks again very much,

Chris
0
Ray PaseurCommented:
That's fine by me -- glad you got a good solution, and thanks for the points. ~Ray
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
Apache Web Server

From novice to tech pro — start learning today.