Forum Moderators: phranque

Message Too Old, No Replies

RewriteRule problem

         

magnum

4:27 pm on Sep 2, 2005 (gmt 0)

10+ Year Member



Hi,

I am using this in htaccess file to add banner to all html pages:

RewriteEngine On
Options +FollowSymlinks
RewriteBase /public_html/
RewriteCond %{REQUEST_FILENAME}!header.php
RewriteCond %{REQUEST_URI} ^(.*)/$ [OR]
RewriteRule ^(.*)\.html$ [mydomain\.com...] [R,L]

It works, but the web browsers's shows this url as:
[mydomain.com...]

, and all http links in index.html like [mydomain.com...]
instaed of
[mydomain.com...]

Can anyone help me?

Regards

jd01

5:36 pm on Sep 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think there are a couple of more effective way to do this:

1. Use SSI and at the top of pages EG

<!--#include virtual="/path/to/your/banner.gif" -->

2. Use php auto_prepend_file EG

Added: For php V4 below - you will have to define the version you are running for changes to take effect.

<IfModule mod_php4.c>
php_value auto_prepend_file "/path/to/your/banner.gif"
</IfModule>

If you had to you could add a handler type to handle the html files as php (It will cost you some processing overhead, so be careful.)

Mod_Rewrite is *not* designed to do what you are attempting, and therefore creates a cumbersome, messy solution. I highly recommend using one of these two instead, which are designed for exactly what you are doing.

If you need help implementing them, let us know.

Hope this helps.

Justin

magnum

6:16 pm on Sep 2, 2005 (gmt 0)

10+ Year Member



but this is a free hosting service, so I am unable to edit every web page....

jd01

6:34 pm on Sep 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



With the php solution, you can do it all through .htaccess, you do not need to ever access a page, you just need to parse them as php and use the prepend. The php engine will put your code on the top of every page it parses as php.

Justin

Edit: Corrected Apache to php engine

magnum

7:12 pm on Sep 2, 2005 (gmt 0)

10+ Year Member



Thanks for the message Justin, but I dont want to give a script engine to free host users, they must upload and work only with static html pages only ...

jd01

10:23 pm on Sep 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



About the only thing I can think you might try is:

RewriteEngine on
RewriteCond %{QUERY_STRING} !.
RewriteRule ([^.]+)\.html$ /header.php?file=%{REQUEST_FILENAME} [L]

This should create an internal redirect if there is no query string, and silently serve the information from the new location to the requested location. Not ideal, but might work.

Justin

magnum

1:06 pm on Sep 3, 2005 (gmt 0)

10+ Year Member



I got a same error with your code, maybe a mistake in my php file?

<?php
$location = $_GET['file'];
.............
........
print $banner;
readfile($location);
?>

I tried a 'header("Location: ")' header instead of `readfile` function, but got a 'maxredirects' loop apache error ...

jdMorgan

3:42 pm on Sep 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You might want to check that $_GET['file'] is returning a non-blank and different path before taking action...

Jim

jd01

5:02 pm on Sep 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, I knew what I was thinking when I was typing the Condition, but guess I forgot to share...

You will need to add a query string to the requested URL to break the loop.

$location = $_GET['file']."?anything";

Should have no effect on the file, but is the only way (I can think of) to tell if the URL has been processed by your script or not.

Justin