Forum Moderators: phranque
RewriteEngine on
RewriteRule !\.(js¦ico¦gif¦jpg¦png¦css¦svg¦php¦rtf)$ index.php
I am wanting, for example, a url such as 'www.example.com/foo/bar' to rewrite to 'www.example.com/index.php', but I am getting a 404 not found.
Looking at the error logs it is trying to access 'www.example.com/foo'
If I try a url with more directories, it will always rewrite to just the first directory.
If I try and access a file such as 'www.example.com/foo' or even just 'www.example.com' it rewrites to 'www.example.com/sys_cpanel'
Is there anything I can do to make this work myself? Why is this happening?
Many thanks
Narius
In order to resolve this problem, you need to re-arrange the code, and then create and use a"back-reference":
RewriteCond %{REQUEST_URI} !\.(js¦ico¦gif¦jpg¦png¦css¦svg¦php¦rtf)$
RewriteRule ^(([^/]+/)*) /$1index\.php [L]
However, this still leaves open the question of what you want to do with the filename and filetype (if present) if they do *not* indicate an excluded media, css, or php filetype; This modified rule will still drop the filename and filetype.
If your server is configured to use AcceptPathInfo (Apache 2.x and higher only) then you will likely be able to retrieve the filename and filetype from "PathInfo" within your script. If not, then you will have to decide what mechanism you want to use to pass the filename and filetype to your script -- For example, passing the filename/filetype as a query parameter to the script using the GET variable in the script to retrieve it.
Important: Replace the broken pipe "¦" characters with solid pipe characters before use; Posting on this forum modifies the pipe characters.
Completely-flush your browser cache before testing any new server-side code.
Jim
I want everything rewritten to 'www.example.com/index.php' whether it be 'www.example.com' or 'www.example.com/foo/bar/something/else' unless of course it is a file with an extension that is on the white-list.
Instead what is happening is 'www.example.com/foo/bar/something/else' is being rewritten to 'www.example.com/foo', but what I am expecting, and want, is for it to be rewritten to 'www.example.com/index.php'
This works on my home server, but not on the host I am using.
Thanks again
Narius
You might also try disabling MultiViews, if you are not using content-negotiation:
Options -MultiViews
It may become necessary to disable AcceptPathInfo and use the query-string GET parameter method I described above if your host has configured this machine in some odd way.
If you did not write this index.php script, take a look at it, and determine how it figures out what "page" was requested. That mechanism may need to be adjusted if it isn't finding the right path. In fact, it may even be generating a redirect to the wrong path, which is something your should check with an HTTP headers checker such as "Live HTTP Headers."
Jim
I'm not sure what method it is using. It is whatever the Zend Framework uses.
I was hoping that there would be a simple solution that either I could add to the .htaccess file to get it working, or to be able to email the host with some more specific information.
Thank you very much for all your help
Narius