JEV
JEV

Reputation: 2504

PHP mod_rewire entire site, and rewriting image URLs?

I have pretty much developed an entire site and was foolish to not implement mod_rewrite. I wish to do so now, and did a test page. The mod_Rewrite works perfectly, but I then have the issue of all of the imaegs being linked to the new pages URL, for example:

www.mysite.com/county/Kent.html

from

www.mysite.com/sector.php?county=kent

It works great, but then when I am on the new link the URL for images changes to this.

mysite.com/county/images/widMot.png

As if the 'images' folder is in the 'county' folder which actually doesn;t exist. Is there a rewrite rule, which will ignore this image link change? Or something I am missing?

EDIT: As requested:

...
RewriteEngine On
RewriteRule ^county/([^/]*)\.html$ /sector.php?county=$1 [L]
...

EDIT: The solution the answer posted was excellent, and resolved the image issue, but I was then stuck with an issue of my css files being put after /images/ directory. Ofc a simple fix would be place them in there, but is there no other way?

Upvotes: 0

Views: 173

Answers (2)

Victor Nițu
Victor Nițu

Reputation: 1505

Please take a look at the <base> HTML tag.

http://www.w3schools.com/tags/tag_base.asp

It allows you to define the base URL of your site, after that, any link built like /images/someimage.png will direct you to the root directory's images/.

EDIT: To be more specific, all relative URLs starting with a forward slash / will begin immediately after domain name.

Upvotes: 1

Celos
Celos

Reputation: 517

Try something like:

<IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule YOUR REWRITE RULE
</IfModule>

Should pass everything to your rewrite rule, unless the requested URL is an actual file on the server.

Upvotes: 0

Related Questions