tags:

views:

825

answers:

4

hi there people. I just want to get a quick htaccess redirection. ie:

domain.com/subfolderGreen --> domain.com/index.php?folder=subfolderGreen

(note that the subfolderGreen actually exists)

I've been trying but couldn't get to the regex needed.

thanks. a.

ADDED:

Sorry, i want this to work for any subfolder, not just "subfolderGreen" I'm using

RewriteRule ^/([^/]+)/?$ /index.php?folder=$1 [L]

but it's not working. any clues?

A: 
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /(([^/\ ]+/)*)/+([^\ ]*)
RewriteRule ^ /%1%3 [L,R=301]
RewriteRule ^([^/]+)/?$ index.php?folder=$1 [L]

Line #2 will handle the case if someone goes to website.com/mypage///// so it defaults to website.com/mypage/ instead (I think)

Joe Philllips
That's a rewrite, not a redirect. A rewrite is done on the server, each time a request comes in. That means the client doesn't know about it, so it wastes server resources.
Matthew Flaschen
I interpreted what he said and guessed at what he meant.
Joe Philllips
hey, i need this to work for any subfolder, that will exists
+1 - Seems like a reasonable answer to me (though see his edit about changing it for the general case, which seems like it's going to end badly...)
Dominic Rodger
A: 

I believe it's:

RedirectMatch 301 domain.com/(.*) domain.com/index.php?folder=$1
Matthew Flaschen
+1  A: 
RewriteRule ([^/]+)$ index.php?folder=$1

I think that will do the trick.

RewriteRule has some confusing issues when used in .htaccess which requires the addition of a RewriteBase.

What errors/problems are you seeing? If you want to be sure of how it's redirecting adding a [R] can often help with the debugging.

Finally... does the subfolderGreen really exist or not? If it exists that could cause some problems.

James C
yes. the subfolders exists. the folders have images, and the index.php will show them. so the folder has to exists, but i want a central php script for any images subfolder.i didn't followed you on the RweriteBase hint..
Please try the rule above with [L,R] at the end of it. This will allow you to see how the RewriteRule is behaving. Please post back results.When you try the test can you also attempt it with domain.com/NoFolder and see how that behaves?You might find that the server is trying to correct the invalid URL http://domain.com/folder to http://domain.com/folder/ which would miss your rewrite rule.
James C
I've just tried this one, but it goes trough. If I try with any folder name, it gives me a 404, and if i try with a existing folder, it opens it for directory listing or anything. so it doesn't get into the rule. might this be due to being folders instead of files the sources for rewrite?
+1  A: 

I would think your example would cause an endless loop since /index.php matches what you are doing. Try this:

RewriteRule ^([A-Za-z0-9]+)/?$ /index.php?folder=$1 [L]

If you want it to work for all directories that exist, this will probably work as well.

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+)/?$ /index.php?folder=$1 [L]
Chris Bartow
this works great. let me keep betatesting it. thanks
Won't work for website.com/my-directory/ will it?
Joe Philllips
@d03boy just add \- as an additional character after 0-9 to the first line if you have hyphens in your directory names.
Chris Bartow