Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite files without extension even if file with extension exist

rewrite files without extenstion to page.php

         

Tom_V

10:32 pm on Aug 11, 2009 (gmt 0)

10+ Year Member



I'm facing a problem I can't understand.

I'm trying to rewrite all non-existing files to page.php?p=$1
So I tried to use the following code:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9\-\+]+)$ page.php?p=$1 [L]

But now my problem:
why doesn't 'index' rewrite to 'page.php?p=index' ?
'home' rewrites succesfully to 'page.php?p=home'.

What I see as a difference is that index.php exists and home.php doesn't.
So all $1 url's doesn't rewrite if $1.php exists.
If $1.php doesn't exist; It rewrites like it should do.

Caterham

10:59 pm on Aug 11, 2009 (gmt 0)

10+ Year Member



why doesn't 'index' rewrite to 'page.php?p=index' ?

Because you (or your host) enabled (by accident?) MultiViews (provided by mod_negotiation) in an Options directive.

[httpd.apache.org...]

mod_negotiation registers a type_checker hook, which runs prior mod_rewrite's fixup hook. Your request was internally fast redirected to index.php and your regular expression does not match against index.php. That's why your rule failed to match.

Tom_V

12:38 am on Aug 12, 2009 (gmt 0)

10+ Year Member



OMG tyvm looked like hell for it and couldn't find it.

Options -MultiViews fixed the problem :)

Tom_V

2:02 pm on Aug 23, 2009 (gmt 0)

10+ Year Member



OK, next problem:

DirectoryIndex page.php?p=home

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9\-\+]+)$ page.php?p=$1 [QSA,L]

Going to /?style=4 redirects to /page.php?p=home?style=4
As you see a double ? so style isn't another query, is this possible to fix?

jdMorgan

3:51 pm on Aug 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Looks like your Apache install is broken then, because your rule should not rewrite "/?style=4" at all: Your rule requires that the requested URL-path be non-blank, e.g. example.com/<something-here>?<any-or-no-query-here> in order to be invoked.

And if that requested URL-path is non-blank (e.g. as a result of the action of the preceding DirectoryIndex directive), then your rule requires that that URL-path *not* resolve to a physically-existing file or directory, and that that URL-path contain only alphanumeric, hyphen, or plus-sign characters (i.e. a request for page.php won't match this pattern, because it contains a period and because page.php exists).

Note that in the first line of this post, I said that your rule should not "rewrite" -- If you are instead seeing a redirect (i.e. the browser address bar changes), then you've likely got another (faulty) rule invoking an external redirect, or one of your scripts is invoking an external redirect. I suppose this might be better news than being told you need to re-install Apache, though... :)

Jim

Tom_V

4:48 pm on Aug 23, 2009 (gmt 0)

10+ Year Member



:(

First for clarifying: It rewrites and doesn't redirects ;)
Second it's a hosts server <snip>
Thirt you're right about the fact it shouldn't rewrite at all.

So I don't think asking the host for re-install they will listen xD

And as it's only a little error (wich normally never happens), I don't gonna put much more time in it. Except as you "mini-experts", knows why it happens and how to fix it.

Anyhow already thanks for the answer

[edited by: jdMorgan at 6:15 pm (utc) on Aug. 23, 2009]

jdMorgan

6:16 pm on Aug 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You should definitely fix this problem, even if it only happens occasionally.

This is a time-bomb which might destroy the ranking of your site.

Jim

Tom_V

8:40 pm on Aug 23, 2009 (gmt 0)

10+ Year Member



Hmm, it happens because of : "DirectoryIndex page.php?p=home"
So not because of mod_rewrite

(BTW I tested it on my test-server and it got the same result)

jdMorgan

8:53 pm on Aug 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can get rid of that DirectoryIndex directive and replace it with a rewriterule if you like:

# Replace "DirectoryIndex /page.php?p=home" directive
RewriteRule ^([^/]+/)*$ /$1page.php?p=home [L]

Put this ahead of your existing rule.

Jim

Tom_V

1:49 am on Aug 24, 2009 (gmt 0)

10+ Year Member



Thx, works perfect, just changed [L] to [QSA,L] and removed the first /

You guys are really just great!