Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite problem on server

RewriteRule rewriting to unspecified location

         

Narius

6:59 pm on Dec 12, 2008 (gmt 0)

10+ Year Member



Hi, I was wondering if anybody here could give me some information. I have a very simple rewrite which I have placed in the public_html directory on the host.


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

jdMorgan

7:31 pm on Dec 12, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's happening because mod_rewrite is doing what you told it to do; Rewrite all requests for URLs not resolving to media files, css, or php to the single filepath "index.php".

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]

This causes the directory path from the requested URL to be captured and then prepended to the "index.php" filepath, so that /foo will be rewritten to /index.php, /foo/bar will be rewritten to /foo/index.php, and /foo/bar/fly/two will be rewritten to /foo/bar/fly/index.php

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

Narius

8:02 pm on Dec 12, 2008 (gmt 0)

10+ Year Member



Sorry, my mistake, I should have been more explicit before.

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

jdMorgan

8:52 pm on Dec 12, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So you are using PathInfo, right?

You might also try disabling MultiViews, if you are not using content-negotiation:


Options -MultiViews

If that doesn't help, I'd be looking for other mod_alias or mod_rewrite directives in the server config and in other .htaccess files, since the URL-path isn't likely to disappear on it's own...

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

Narius

9:21 pm on Dec 12, 2008 (gmt 0)

10+ Year Member



Thank you, but still no luck.

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

jdMorgan

10:00 pm on Dec 12, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could also try a Zend-specific forum, since there will likely be more users there who have dealt with such problems, and they are more likely to 'speak Zend' than our average contributor here.

Jim