1

What I've been trying to achieve is to get my .htaccess file to rewrite ALL URLS.

No matter what I do, however, I cannot get it ignore existing directories. And by that, I mean, act as if they don't exist.

For example, say I have the following file structure:

/
    dir1
        file1.php
    dir2
        file2.php
    .htaccess

And suppose I want to redirect all traffic to dir1/file1.php?url=path.

This never works for me if the path is an existing directory.

For example, if I navigate to url/path/stuff/dir2, the "redirecting" works, but the URL in the address bar changes to url/path/stuff/dir2/?url=dir2 for some unfathomable reason.

Here is my .htaccess:

Options -MultiViews

Options -Indexes

Options +FollowSymLinks

# so navigating to a url with a trailing slash won't cause problems
DirectorySlash Off

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^(.*)$ dir1/file1.php?url=$1 [QSA,L]
kukac67
  • 298
  • 1
  • 3
  • 12
  • 1
    Have you shown us all of your .htaccess? Do you have a line it that is something like: RewriteCond %{REQUEST_FILENAME} !-f As it stands, your .htaccess should loop infinitely. – BareNakedCoder Dec 11 '15 at 19:18
  • 1
    @BareNakedCoder Ah! That explains many things I didn't understand. But I added a check and it still does not change behavior. (I edited the question though.) – kukac67 Dec 11 '15 at 19:22
  • if you dont want the `?url=dir2` in the address bar but still want to append the data to the `$_GET["url"]`, try removing the `QSA` – Andrew Dec 11 '15 at 20:09
  • heres two post that I find might related to your problem, http://stackoverflow.com/questions/990392/htaccess-rewrite-to-redirect-root-url-to-subdirectory, http://stackoverflow.com/questions/12551382/what-does-1-qsa-l-mean-in-my-htaccess-file – Andrew Dec 11 '15 at 20:11
  • @Andrew I just don't understand why the address bar is being changed, as I am not doing any redirects. These questions are helpful in understand what the code is doing, but I still cannot understand why the address bar is being changed. – kukac67 Dec 11 '15 at 20:23
  • With `DirectorySlash Off` you won't get trailing slash redirect. Are you sure there is no other .htaccess in your system? – anubhava Dec 11 '15 at 22:43

1 Answers1

0

You (updated) .htaccess is saying: IF (it is not an existing file) THEN { rewrite the URL }. Seems like you want

RewriteEngine on
RewriteCond %{REQUEST_URI} !dir1/file1.php
RewriteRule ^(.*)$ dir1/file1.php?url=$1 [QSA,L]

In this case, the RewriteCond just prevents infinite looping. The RewriteRule gets applied once and only once. Rewrite module will keep cycling thru all the rules until a cycle does not result in a changed to the URI.

BareNakedCoder
  • 3,257
  • 2
  • 13
  • 16
  • Yes, thank you. This also makes things clearer. But the main problem still persists: navigating to `path/dir2` makes the address bar change to `path/dir2/?url=dir2` while I don't want it to change. (It still "shows" `dir1/file1.php` though, so that's good.) – kukac67 Dec 11 '15 at 19:40
  • Did you try it? Rewriting to have url=dir2 is done internally to Apache only and shouldn't show up in the browser address bar (unless you actually redirect to that, which you are not doing since you don't have [QSA,L,R]). All framework use this trick to invoke their front-end controllers to initially handle requests of all forms. – BareNakedCoder Dec 11 '15 at 19:46
  • Yeah, I'm trying all sorts of different variations, but the address bar always changes to `path/dir2/?url=dir2`. – kukac67 Dec 11 '15 at 19:47
  • So I did some logging, and there is one line that seems to be the error, this appears in the logs: `split uri=dir1/file1.php?url=dir2 -> uri=dir1/file1.php, args=url=dir2&url=dir2` and I don't understand why. – kukac67 Dec 11 '15 at 21:55