Forum Moderators: phranque
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
Welcome to WebmasterWorld!
Here's a piece of the puzzle to get you started:
RewriteCond %{REQUEST_FILENAME} -f
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
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?
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
You could force a test case and then check the server error log to confirm. [/added]
Jim
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
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME}!-f
causes 500 internal server error.
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
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).