Forum Moderators: phranque

Message Too Old, No Replies

Simple mod_rewrite

mod_rewrite Rewriting only nonexistent URLs.

         

trevordixon

2:42 am on Jan 6, 2006 (gmt 0)

10+ Year Member



I have the following rewrite rule:
RewriteRule ^/([^/.]+)\.html$ /loadpage.php?filename=$1 

How can I make the server rewrite [filename].html to
loadpage.php?filename=[filename] only when [filename].html does not
exist on the server?

Example: /index.html exists but /page.html does not.
[domain.com...] will show /index.html, while
[domain.com...] will be rewritten to
/loadpage.php?filename=page because page.html does not exist.

Thanks to anyone who will be willing to help me.

Trevor

jdMorgan

3:10 am on Jan 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Trevor,

Welcome to WebmasterWorld!

Here's a piece of the puzzle to get you started:


RewriteCond %{REQUEST_FILENAME} -f

See the 'flags' section following the RewriteCond directive description in the Apache mod_rewrite documentation [httpd.apache.org].

Be aware that you are requiring extra calls to the filesystem to check the files' status if you do this, thus increasing the server's workload; If your site is very busy, make sure to do this only when necessary. I suggest that you bypass the file exists check if the filetype should not be handled by your script. For example, perhaps you can exclude CSS, image files, and scripts from this check by filetype.

Jim

trevordixon

2:37 am on Jan 13, 2006 (gmt 0)

10+ Year Member



What the heck is wrong with this?


RewriteEngine On
RewriteCond %{REQUEST_URI} !-f
RewriteRule ^/([^/.]+)\.html$ /test.php?filename=$1 [L]

It's rewriting EVERY request, even for files that do exist. When I try -f instead of !-f, it doesn't rewrite any requests. I've also tried -F, !-F, %{REQUEST_FILENAME}, and every combination of the mentioned possibilites and every time I use the ! it rewrites everything, and without the ! it won't rewrite anything. What's going on?

jdMorgan

6:10 am on Jan 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Files are files, URLs are URLs, not equivalent or even necessarily related... :)

RewriteCond %{REQUEST_[b]FILENAME[/b]}!-f

Jim

trevordixon

11:24 pm on Jan 19, 2006 (gmt 0)

10+ Year Member



I can't figure out why this won't work!


RewriteEngine On
RewriteCond %{REQUEST_FILENAME}!-f
RewriteRule ^/([^/.]+)\.html$ /test.php?filename=$1

If there's nothing wrong with the code above, where else do I need to look for problems?

jdMorgan

11:28 pm on Jan 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If your code is for use in .htaccess, as opposed to httpd.conf, then remove the leading slash on the RewriteRule pattern. URLs 'seen' by RewriteRule in a per-directory (.htaccess) context are localized to that directory, therefore the leading slash will have been stripped.

Jim

trevordixon

11:57 pm on Jan 19, 2006 (gmt 0)

10+ Year Member



It's in httpd.conf. Is there anything that might be causing %{REQUEST_FILENAME} to not return the correct filename?

jdMorgan

12:23 am on Jan 20, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, but make sure you've got the code in the right <container> so that it is activated for the relevant requests.

Also if on Apache 1.x, make sure that mod_rewrite is loaded after php, so that it runs before php -- the execution order is the inverse of the LoadModule order on Apache 1.x.

[added] Occasionally, it is necessary to use


RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f

to get this to work.

You could force a test case and then check the server error log to confirm. [/added]

Jim

trevordixon

1:12 am on Jan 20, 2006 (gmt 0)

10+ Year Member




RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME}!-f
RewriteRule ^/([^/.]+)\.html$ /test.php?filename=$1

That worked! I just needed %{DOCUMENT_ROOT}. Thank you very much.

adamsfather

9:35 am on Jan 20, 2006 (gmt 0)

10+ Year Member



I have the same problem, but following this thread nothing changed.

Im developing site on my local computer with Apache2/PHP on windows xp using .htaccess with following rewrite rule:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^page=([^&]+)$
RewriteRule ^index\.php$ %1.html? [R=301,L]
RewriteRule ^([^/]+)\.html¦htm /index.php?content=$1

working without any problem. Whole site is just one script and urls generated from db are rewritten for getting content. URL's are non existent :).

Installing it on testing remote server Apache/2.0.51 (Fedora)/Limux/PHP with same rewriting rules I get 404, not found. It is dedicated server which uses virtual host, so each host has its own httpd.include configuration file. I'm not webmaster and that dedicated server has no tech support, so I'm getting lost with that problem. I tried all settings of httpd.conf and related httpd.include file to get it run but no success.

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME}!-f
causes 500 internal server error.

Any help would be appreciated.
Stan

extras

5:53 pm on Jan 21, 2006 (gmt 0)

10+ Year Member




RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME}!-f
causes 500 internal server error.

You need a space between %{REQUEST_FILENAME} and !-f.

I thought %{REQUEST_FILENAME} is a full path and you don't need the %{DOCUMENT_ROOT},
while %{REQUEST_URI} is a path from doc root and you need %{DOCUMENT_ROOT}.

So, I'd try this:


RewriteCond %{REQUEST_FILENAME} !-f

or this:

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f

Note:

The missing space might be just in your post, though...
I found that this forum removes single white space from the post.
So, I used two spaces (as it doesn't hurt in .htaccess).

adamsfather

8:53 am on Jan 23, 2006 (gmt 0)

10+ Year Member



Thanks trevordixon for your help;

this :
RewriteRule (.*\.html¦.\htm) index.php?content=$1

fixed whole problem, i have removed all previous rules and conditions and it worked.

thanks again

extras

3:15 pm on Jan 23, 2006 (gmt 0)

10+ Year Member



You can write it like this, if that is what you needed.
RewriteRule (.*\.html?) index.php?content=$1