Jump to content

Show files in sub folder when accessing them from first folder?


shedokan

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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/

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.