1

The IfModule does fix the slash problem for all links. All links get a / behind it.

<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
</IfModule>

To fix the / behind folders only use and not the code above here. Does not work in ALL browsers (IE for example).

Options +FollowSymlinks -MultiViews
DirectorySlash On

I was using the following code [orginal post].

Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(website.com)(:80)? [NC]
RewriteRule ^(.*) http://www.website.com/$1 [R=301,L]

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

The original link is /files.php?sub=page1 and changes to /files/. This works fine.

But if I type /files into the url it goes to filesfiles/?sub=page1.

How can I make /files (without the end slash) also go to /files/ and for the folder only.

I tried a lot of things, but I can't get it right.

<?php
// Load pages
$sub = strtolower($_GET['sub']);
//
if(strlen($sub) > 0){
if(file_exists('files/'.$sub.'.php')){
    include('files/'.$sub.'.php');
}
    else{
        echo "<h1 class=\"titel\">Page not found.</h1>";
        echo "<p>The page doesn't exist or hasn't been found.</p>";
    }
}
else{
    include('files/page1.php');
}
?>

page1.php loads fine to /files/ and when using the url /files it return to files/?sub=page1 page2.php is linked like /files/page2 and shows fine.

There is only a problem with the page1 if I don't want to show page1 in the url. The url like /files/page1 works fine, but I want it like /files/

domain.com/files.php?sub=page1 loads the content fine.

domain.com/files/?sub=page1 also loads the content fine.

  • "The IfModule does fix the slash problem..." No, it doesn't. IfModule merely says "run this _section_ if the mod_rewrite module has been installed". The code between the `` is what does the magic. – Phil Perry Dec 18 '13 at 15:08

1 Answers1

1

Make trailing slash optional in your rules as:

Options +FollowSymlinks -MultiViews
DirectorySlash On

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^files/$ /files.php? [L]

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^files/([A-Za-z0-9-]+)/?$ /files.php?sub=$1 [L,QSA]

Also important is this line Options -MultiViews which turns of MultiViews option. MultiViews if turned on might conflict with your rewrite rules.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • I added the Multiviews and added your suggestion of /?$ but I still get the same: files/?sub=page1 I also update the topic start. – user3114929 Dec 18 '13 at 11:21
  • HIs files an actual directory? – anubhava Dec 18 '13 at 11:24
  • These files are in a real folder yes. Anyway if I type files without the slash it does add a slash to the end (what I want), but also ?sub=page1 – user3114929 Dec 18 '13 at 11:25
  • Ah ok, you didn't tell this fact in your question. Let me edit it – anubhava Dec 18 '13 at 11:26
  • Okay, sorry :) didn't know that was important. – user3114929 Dec 18 '13 at 11:26
  • Have you tried this in a different browser? Also do you have any .htaccess in `/files/` directory OR an other rules in this current .htaccess? – anubhava Dec 18 '13 at 11:43
  • Yes, I have some extra commands. Start post updated. No other .htaccess in the folder files. Also other browsers give the same problem. – user3114929 Dec 18 '13 at 11:48
  • I just SOLVED the problem. Found a little piece of code... :) – user3114929 Dec 18 '13 at 12:03
  • Yes I just came back and detcted that your trailing slash addition rule is indeed problematic. Good to know you solved the problem. – anubhava Dec 18 '13 at 12:06
  • Also now that your problem is solved you can mark the answer as "accepted" (that gives your some reps also) – anubhava Dec 18 '13 at 12:10
  • It works better, but gives some other problems. It shows page hasn't been found if i try to access /files/ and /files/page2 and /files/page3 do work fine. So files/page1 also works, but only /files/ now gives an error. page1 should redirect as only one to /files/ – user3114929 Dec 18 '13 at 12:14
  • Can you try commenting out: `RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]` rule? – anubhava Dec 18 '13 at 12:55
  • Anyway the IfModule mod_rewrite.c caused to many problems for my website. I should have changed al links in the code. The code does work, but not for me. I am not going to change all links inside the code, because it adds a slash everywhere behind. – user3114929 Dec 18 '13 at 13:26
  • But after changing back to my default .htaccess it looks like it has been solved. I am still using the -MultiViews and DirectorySlash On. That would be the only things that have been changed in my file at the moment and it WORKS! We took a long and bumpy ride, but it has been solved. Thank you for your help! :) – user3114929 Dec 18 '13 at 13:27
  • You're most welcome, yes that trailing slash rule was indeed the cause behind this. – anubhava Dec 18 '13 at 13:33
  • It only works in Opera. IE and FF still give /files.php?sub=page1 when using files without a slash behind it. – user3114929 Dec 18 '13 at 14:09
  • Clear your browser cache in FF – anubhava Dec 18 '13 at 14:12
  • Still the same after clearing browser cache. I think it wasn't solved. Correct, if I empty the cache with Opera the problem is back. So not solved :(. – user3114929 Dec 18 '13 at 14:18
  • Is the problem only with `http://domain.com/files` URL? OR with any other URL also? Also do you have `DirectorySlash On`? – anubhava Dec 18 '13 at 14:22
  • Yes, DirectorySlash is on. Also other urls. All the urls with /a-folder have the same problem. – user3114929 Dec 18 '13 at 14:29
  • No error, it shows the website correctly, but the link changes to http://domain.com/folder/?sub=includedpage – user3114929 Dec 18 '13 at 14:35
  • Can you open this URL in Firebug and see what you get in `Net` tab for this request. – anubhava Dec 18 '13 at 14:39
  • What url must be opened in Firebug? – user3114929 Dec 18 '13 at 14:40
  • btw what is `includedpage` text is in this `http://domain.com/folder/?sub=includedpage` URL? – anubhava Dec 18 '13 at 14:42
  • Yes, I did. The response is the same as opening /folder/. Take a look at the topic start I added the PHP code there. That shows what I mean with includedpage. – user3114929 Dec 18 '13 at 14:42
  • Didn't find any reference to `includedpage` in the question – anubhava Dec 18 '13 at 14:46
  • includedpage was only a example/description. Tt works like this: domain.com/files.php?sub=page1 loads the content fine. domain.com/files/?sub=page1 also loads the content fine. – user3114929 Dec 18 '13 at 14:53
  • I use a files.php in the public_html and inside the public_html there is a folder called /files/ inside the folder /files/ I placed all the .php pages that should load by the PHP script. Like page1.php page2.php and if(file_exists('files/'.$sub.'.php' includes the text. – user3114929 Dec 18 '13 at 15:00
  • I hope this makes it more understandable to you :) – user3114929 Dec 18 '13 at 15:03
  • yes it does help. Let me retest it locally by creating exactly same setup as yours. Stay tuned :) – anubhava Dec 18 '13 at 15:37
  • Great :). If I change the DirectorySlash to Off then /files without the slash gives the response of the PHP echo "

    The page doesn't exist or hasn't been found.

    "; and doesn't go to files/?sub=page1 anymore. Maybe that info helps.
    – user3114929 Dec 18 '13 at 15:40
  • Hmm looks like a cache problem again. But somehow after cleaning cache it still keeps showing. Really need to test with a different browser. The DirectorySlash Off doesn't give the response I told in my post here before. – user3114929 Dec 18 '13 at 15:51
  • Right and `DirectorySlash Off` is never a solution or anything. It is just a hack and very unsecured. – anubhava Dec 18 '13 at 15:52
  • I see. I am trying all kind of things. Been messing around with this bug for weeks. Nice learning curve. :) – user3114929 Dec 18 '13 at 15:57
  • Couldn't recreate the problem but I have made some edits to my answer. – anubhava Dec 18 '13 at 16:32
  • ok one last update... if this doesn't work then I want you to enable `RewriteLog` – anubhava Dec 18 '13 at 16:46
  • Clear browser cache and as I said enable `RewriteLog` for further debugging – anubhava Dec 18 '13 at 16:50
  • Yes, I did clear browser. Doesn't work. Sorry. How do I enable Rewritelog? – user3114929 Dec 18 '13 at 17:03