shedokan Posted December 25, 2009 Share Posted December 25, 2009 How can I make the files in a sub folder to be shown when I try to access their names in the parent folder? I mean like: /directory1/file.txt will show: /directory1/directory2/file.txt I've tried: Options +FollowSymLinks RewriteEngine on # handle domain root and skip subfolders RewriteCond %{REQUEST_URI} ^/folder1/ RewriteCond %{REQUEST_URI} !^/folder1/folder2/ RewriteRule ^(.*)$ folder1/folder2/$1 [L] # add trailing slash to subfolders (eg abc to: abc/) RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} ^/folder1/ RewriteCond %{REQUEST_URI} [^/]$ RewriteRule ^(.*)$ $1/ [L,R=301] # handle files in subfolders RewriteCond %{REQUEST_URI} ^/folder1/ RewriteCond %{REQUEST_URI} !^/folder1/folder2/ RewriteRule ^(.*)$ folder1/folder2/$1/ [L] Which I found on some forum, and all I get is a 404. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/186349-show-files-in-sub-folder-when-accessing-them-from-first-folder/ Share on other sites More sharing options...
cags Posted December 26, 2009 Share Posted December 26, 2009 Trying sticking an R in this line... RewriteRule ^(.*)$ folder1/folder2/$1 [L,R] Just to see what the URL changes to, this will probably help explain where the rule is failing. If you can give us an example of what your inputting and show us where your ending up, I'm sure we can work something out. Quote Link to comment https://forums.phpfreaks.com/topic/186349-show-files-in-sub-folder-when-accessing-them-from-first-folder/#findComment-984182 Share on other sites More sharing options...
shedokan Posted December 26, 2009 Author Share Posted December 26, 2009 My .htaccess file is inside folder1, and I'm inputing: http://localhost/folder1/ With your code I'm getting: http://localhost/C:/xampplite/htdocs/folder1/folder2/folder1/ I've changed the code to: RewriteRule ^/folder1/(.*)$ /folder1/folder2/$1 [L,R] And all I'm getting is the file list of folder1. Quote Link to comment https://forums.phpfreaks.com/topic/186349-show-files-in-sub-folder-when-accessing-them-from-first-folder/#findComment-984195 Share on other sites More sharing options...
cags Posted December 26, 2009 Share Posted December 26, 2009 With your code I'm getting: http://localhost/C:/xampplite/htdocs/folder1/folder2/folder1/ Without a RewriteBase I guess that makes sense, normally when that happens I find that a forward slash at the start of second path will fix the problem. As a simple RewriteRule without any RewriteCond's the pattern you have changed it to looks correct with regards to forwarding any requests from folder1 to the same structure within folder2. I only just started learning mod_rewrite, but your RewriteCond makes perfect sense to me "if path starts with folder1 and doesn't point to folder2" then Rewrite. What exactly does your file contain, given the code you posted earlier the first and last code block are identical. What URL are you typing in to test the redirect? Quote Link to comment https://forums.phpfreaks.com/topic/186349-show-files-in-sub-folder-when-accessing-them-from-first-folder/#findComment-984202 Share on other sites More sharing options...
shedokan Posted December 26, 2009 Author Share Posted December 26, 2009 Sorry, I was trying to view a blank index.html file. I only see a 404 if I try to see a file like index.php that's in folder2. but if I put a index.php file in folder one I see it, so probably that code isn't working. Sorry I was telling wrong info, I was putting the .htaccess file in the root, so I tried again your first code inside folder1 and it works, I only added a rewrite condition to stop looping. here's the code: RewriteCond %{REQUEST_URI} !^/folder1/folder2/ RewriteRule ^(.*)$ /folder1/folder2/$1 [L] and I added this code at the start: RewriteCond %{REQUEST_URI} [^/]$ RewriteCond %{REQUEST_URI} !^/folder1/folder2/ RewriteRule (.*) /folder1/%1/ [L,R=301] so the it will add slashes to folders, but somehow it adds slashes to normal files too how can I fix it? Quote Link to comment https://forums.phpfreaks.com/topic/186349-show-files-in-sub-folder-when-accessing-them-from-first-folder/#findComment-984225 Share on other sites More sharing options...
cags Posted December 26, 2009 Share Posted December 26, 2009 RewriteCond %{REQUEST_FILENAME} !-f Says if a file doesn't exist. Since you are wishing to add the slash when a directory doesn't exist is should presumably be !-d. Quote Link to comment https://forums.phpfreaks.com/topic/186349-show-files-in-sub-folder-when-accessing-them-from-first-folder/#findComment-984231 Share on other sites More sharing options...
shedokan Posted December 27, 2009 Author Share Posted December 27, 2009 I just started learning mod_rewritethe day I started this thread, but from what I've seen, this code won't work: RewriteCond %{REQUEST_FILENAME} !-f If I'm not wrong it checks for wheater the filename exists, so if I go to http://localhost/folder1/file.txt it will check if C:\xamppplite\htdocs\folder1\file.txt exists, but since the file is in folder2 the condition returns fale every time(and when I tried it it didn't work for anything). Quote Link to comment https://forums.phpfreaks.com/topic/186349-show-files-in-sub-folder-when-accessing-them-from-first-folder/#findComment-984463 Share on other sites More sharing options...
cags Posted December 27, 2009 Share Posted December 27, 2009 I just started learning mod_rewritethe day I started this thread, but from what I've seen, this code won't work: RewriteCond %{REQUEST_FILENAME} !-f If I'm not wrong it checks for wheater the filename exists, so if I go to http://localhost/folder1/file.txt it will check if C:\xamppplite\htdocs\folder1\file.txt exists, but since the file is in folder2 the condition returns fale every time(and when I tried it it didn't work for anything). Your not exactly wrong, but that's the whole point of the RewriteCond. It checks in folder1 to see if the requested file exists, if it doesn't exist it applies the RewriteRule. By doing this you can still have for example image.jpg in your folder1\ and access it using folder1\image.jpg. If you didn't have that RewriteCond in place, attempting to access folder1\image.jpg would be Rewritten/forwarded to folder1\folder2\image.jpg. Quote Link to comment https://forums.phpfreaks.com/topic/186349-show-files-in-sub-folder-when-accessing-them-from-first-folder/#findComment-984484 Share on other sites More sharing options...
shedokan Posted December 27, 2009 Author Share Posted December 27, 2009 oh, so instead of checking if it exists in folder2(which is impossible since we can't replace folder1 with folder1/folder2) we check if it doesn't exists in folder1. But the thing is that it still doesn't fixes my last problem, because if I go to folder1/index.php it forwards me to folder1/index.php/ and the file index.php is only in folder2/ Quote Link to comment https://forums.phpfreaks.com/topic/186349-show-files-in-sub-folder-when-accessing-them-from-first-folder/#findComment-984543 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.