Log in

View Full Version : Htaccess not working for sub directories.



punstc
08-10-2010, 02:26 PM
I really new to htaccess and regular expressions, and I'm getting a little lost.

I have a small script that I got from a tutorial that basically removes the file extension and adds a trailing slash to the end if needs be.


RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php

# Forces a trailing slash to be added
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]


This code works fine for files in my main directory. I have a couple files that are in sub directories though and it seems the rules aren't being carried through.

so for files at example.com/file.php the mod rewrite works fine and it allows the url to be example.com/file/

but for files at example.com/sub/file.php if I try to use example.com/sub/file/ I get a 404 page.

I think I understand that the condition its checking against is only looking in the root, but I don't know enough to add in another condition or rule to check a sub directory as well.

any help would be greatly appreciated.

azoomer
08-10-2010, 10:12 PM
I am really on thin ice here, but since no one else suggests anything I thought of asking if you have tried to put a copy of the .htaccess file inside the subdirectory ? I know you need to do that sometimes but I don't know if it will work in this case.

punstc
08-10-2010, 10:14 PM
Yes, I did try that. One of the first things I thought about actually.

What happened was that it removed the sub directory part from th url.

so example.com/folder/file/ would become example.com/file/ and i'd get the 404 again

azoomer
08-10-2010, 10:34 PM
then you have probably also seen this post (http://stackoverflow.com/questions/1068595/htaccess-code-to-remove-extension-and-addforce-trailing-slash)
I have no idea what the hieroglyphs mean, but it seems like they are talking about the same stuff

punstc
08-11-2010, 01:25 AM
I look into that post, they are having the same problem, yet still no real solution with the directories.