Log in

View Full Version : .htaccess Redirection Problems



George1024
02-18-2007, 09:24 PM
Hello everybodies!
I've created a dynamic image that my website users can put in their forum signature, but most forums don't support a .php file as an image.
The image is http://cybergeorge.freehostia.com/greenhat/avatar/?user=George (actually index.php?user=George), and I wanted it so that when the user types http://cybergeorge.freehostia.com/greenhat/avatar/George.gif it will redirect to the original file (George.gif doesnt actually exist).
I tried using a .htaccess file, but George.gif AND all other files (existent or not) come up with 500 Internal Server Error.
The contents of the .htaccess file is:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.+)\.gif$ index.php?user=$1 #[r=301,nc]
I'm pretty good at PHP, but fairly new to .htaccess. Could someone help me please?

George1024
02-19-2007, 06:58 AM
Anyone? No? :(

Shotgun Ninja
02-19-2007, 02:18 PM
Nope. Doesn't look like it. Not very many people on here know .htaccess that well, aside from accompaniments to php-based webpage functions. I only very recently learned what it is. Try the PHP forums. Or ask Twey.

BLiZZaRD
02-19-2007, 08:10 PM
You could just use the single line .htaccess Redirect rule.



Redirect /olddirectory/oldfile.html http://yoursite.com/newdirect


This is important to note the 3 separate parts, the command (Redirect), the old file location and the new file location. The new file location MUST be a complete url, while the old one can be a partial. This is also all on one line with each of the 3 parts separated by a single space.

George1024
02-19-2007, 08:24 PM
Yeh, but it needs to be partial on the source and the destination URLs, because George.gif is meant to redirect to index.php?user=George, and Blah.gif is meant to redirect to index.php?user=Blah aswell.

ItsMeOnly
02-19-2007, 09:22 PM
I think the regexp is wrong, (.+) specifically, anyway, from what I use and works:

RedirectMatch /(.*).gif http://yoursite.com/index.php?user=$1

if the * is too radical, just make it to [0-9a-zA-Z_]

BLiZZaRD
02-19-2007, 09:50 PM
Yeh, but it needs to be partial on the source and the destination URLs, because George.gif is meant to redirect to index.php?user=George, and Blah.gif is meant to redirect to index.php?user=Blah aswell.


If 'Me's advice doesn't help, how many images do you have that need redirecting? Because you can use the single line one like I showed for each file, being very specific for each one. However, if you have a butt-ton of them it would be pointless :)

George1024
02-20-2007, 05:37 AM
Yeh, every user that signs up to my website gets their own dynamic image so that wouldn't really work.

Me's advice was the closest I've got to success :)
But it redirects to http://cybergeorge.freehostia.com/greenhat/avatar/index.php%3fuser=greenhat/avatar/George instead of http://cybergeorge.freehostia.com/greenhat/avatar/index.php?user=George.
I tried removing the url in the .htaccess file

RedirectMatch /(.*).gif http://cybergeorge.freehostia.com/greenhat/avatar/index.php?user=$1
But that just comes up with the Internal Server Error again.

ItsMeOnly
02-20-2007, 07:52 AM
try doublequotes in redirect then:
RedirectMatch /(.*).gif "http://cybergeorge.freehostia.com/greenhat/avatar/index.php?user=$1"

George1024
02-20-2007, 08:20 AM
Hmmmmmmm, the quotes didn't seem do change anything.

ItsMeOnly
02-20-2007, 08:46 AM
what version of apache is it?

You might try last resort:

RedirectMatch /(.*).gif http://cybergeorge.freehostia.com/greenhat/avatar/index.php\?user=$1
That should do it, but if you have 1.3.33 and later, the question mark should work without problems.

Shotgun Ninja
02-20-2007, 02:01 PM
Try not using $1, try using $usr or something instead, where $usr is declared in the PHP as the user. Maybe something like:



RedirectMatch /($usr).gif "http://cybergeorge.freehostia.com/greenhat/avatar/index.php?user=$usr"

Yeah, I know very little about .htaccess, but that MIGHT work.

BLiZZaRD
02-20-2007, 07:30 PM
From what I know, and itsMeOnly will know more..

there shouldn't be any quotes in the urls... just a single space between all elements (at least when dealing with a single line string like that.

George1024
02-20-2007, 08:38 PM
Hmmmm, with the $usr thing, is that meant to be defined in the PHP?
Its still redirecting to http://cybergeorge.freehostia.com/greenhat/avatar/index.php%3fuser=greenhat/avatar/George, and my Apache version is 1.3.33 :confused:
I had another idea, would this work:

AccessFileName htaccess.php
And make a file by that name which gets all the users from the database and prints out their own entry? But that still doesnt fix the question mark problem.

ItsMeOnly
02-20-2007, 09:38 PM
Turns out it should be RewriteRule after all, made some more experiments and George1024 is right, sometimes it just doesn't work

So...

RewriteRule ^(.*).gif http://cybergeorge.freehostia.com/greenhat/avatar/index.php?user=$1

George1024
02-21-2007, 04:22 AM
Oh well, these things happen, it wasnt absolutely necessary anyway lol.

Thanks for the help guys ;P

sam212
04-02-2007, 03:56 AM
I want to redirect url at my web.
if use link http://www.example.com/ben where ben wasn't file, and I want to this redirect to http://www.example.com/index.php?id=ben

i have tried Me's advice but error. Error said I don't have permission to access this index, dan it redirect to http://www.example.com/index.php%3id/index.php%3id/index.php%3id/index.php%3id=ben

Thanks

George1024
04-03-2007, 07:50 AM
I actually found the answer a while ago but didn't post it.
But since someone needs the answer:



RewriteEngine on
RewriteRule ^([^/\.]+)/?$ /index.php?id=$1 [L]


Save that as .htaccess and put in the same folder as index.php.
Or, you can change /index.php?id=$1 to /blah.php?stuff=$1.