Forum Moderators: phranque

Message Too Old, No Replies

No images and styles after RewriteRule

         

vilkspilks

12:23 pm on May 14, 2007 (gmt 0)

10+ Year Member



Hi all,

I'm trying to write some simple Rewrite rules to redirect visitors from, for example, www.example.com/test/page/sample to www.example.com/test/page.php?p=sample

Here are the contents of .htaccess file:

RewriteEngine on

RewriteBase /test/

RewriteRule ^page/([0-z]*)/?$ page.php?p=$1

The redirect works, php files opens. The problem is no images, backgrounds and css styles are diplayed. They are in /test/style/ folder (all works fine without .htaccess). How can i solve this?

Thanks in advance, vilkspilks

jdMorgan

1:07 pm on May 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There are a couple good ways to fix this - Think about long-term plans and ease of maintenance and pick one:

  • Use server-relative or canonical links to refer to css, external JS and stylesheets, rather than page-relative links; That is, use <img src="/logo.gif>" or <img src=http://example.com/logo.gif"> and not <img src="logo.gif">

  • Modify your code so that images, CSS, and JS requests are not rewritten to the script, but rather to the correct directory path.

    The key is that it is the client (e.g. browser or robot) that resolves relative links, based upon where it "thinks" the referring page is located. In this case, the browser thinks we are in a subdirectory called "page" and will form canonical URLs by prepending "http://example.com/page/" to all page-relative links.

    You can see the problem by looking at your server error log, and seeing how the image filepaths differ from the actual correct filepaths.

    Jim

  • omax

    5:32 am on May 15, 2007 (gmt 0)

    10+ Year Member



    Hello to all!
    I had the same problem and the best way was to use normal relative links and put this code in the <head> of the page :

    <HEAD>
    <base href="http://www.domainname.com">
    ....
    </HEAD>