3

So I've created simple .htaccess file as following to make my webiste more SEO:

# PROTECT FILES
<FilesMatch "\.(htaccess|htpasswd|ini|fla|psd|log|sh)$">
Order Allow,Deny
Deny from all
</FilesMatch>

#TURNED REWRITE ON
RewriteEngine on

#REWRITE CONDITIONS
RewriteRule start index.html
RewriteRule aboutMe about.html
RewriteRule contact contact.html
RewriteRule portfolio portfolio.php
RewriteRule reportage reportage.php

But the problem is that my images don't load properly. I used for them previously:

<?php 
   $files = glob("reportageImages/*.jpg");

   for($i = 0; $i < count($files); $i++){
     $imageDir = $files[$i];
     echo '<li><img src="'.$imageDir.'" alt="reportage"/></li>';
   }
?>

or directly without php:

<img src="reportageImages/lama.jpg"/>

and the directory was xamp/htdocs/MG/reportageImages/(here .jpg file) as I'm using xaamp to test my project.

I'm quite mislead by similar questions asked, as for some problems really sounding as mine works simply <base href="/" />, unfotunately not for me. So I would really appreciate any help from you guys, I'm missing sth and tutorials over Internet don't cover all the topics. What worry me also is if in some way I would come over it, would the solution differ if I moved from local server to hosting?

Added photo to better visualize path problems: filepath screenshot

Photos are counted well in directory ( I do same operation for each one and got relevant numbers of broken images boxes).

11
  • What is the URL you are requesting? Where are your .htaccess, index.html, about.html, etc. files located? What is your document root? xamp/htdocs or /xamp/htdocs/MG? "don't load properly" - do you mean they don't load at all, ie. 404? You imply that it is only since implementing your .htaccess file that the images have stopped loading - but nothing in your .htaccess file should have changed this?
    – MrWhite
    Commented Jul 20, 2016 at 21:47
  • all: .htaccess, .html and .php files are in /MG as well as css folder and js. I have just alt printed instead of image and image broken icon.
    – kolboc
    Commented Jul 20, 2016 at 21:50
  • And what image URL is the browser requesting (check the network tab of the object inspector). Do the CSS and JS files load OK - what is the difference with these URLs? If your document root is xamp/htdocs then the href attribute of the base tag should be /MG/ (or /MG/start), not / - however - it is not obvious from what you have posted why the relative path, with no base tag, is not working.
    – MrWhite
    Commented Jul 20, 2016 at 22:02
  • Failed to load resource: file:///D:/xamp/htdocs/MG/'.$imageDir.', but, $imageDir with this php code should look like reportageImages/image.jpg, so still I don't know what's wrong ... :( css and js files are ok, everything is properly placed and gallery slider is also right
    – kolboc
    Commented Jul 20, 2016 at 22:09
  • 1
    What is the value of the src attribute in the HTML (page source)? You certainly shouldn't be seeing file:///D:/xamp/htdocs/ - that is as if you are trying to load a resource outside of your webserver, from the local filesystem?! Are you actually seeing the text $imageDir in the reported URL?!
    – MrWhite
    Commented Jul 21, 2016 at 0:28

2 Answers 2

1

RewriteRule portfolio portfolio.php
RewriteRule reportage reportage.php

Sorry, just realised what's going on... you need to be more specific in your rewrites. The above rules will rewrite the URL if "portfolio" (or "reportage") appear anywhere in the requested URL - this will catch your image URLs as well (since "portfolio" and "reportage" are part of the URL path), and that's the problem. (I would expect this to also generate a rewrite loop - which would also result in a broken image. In this case the server's error log should give clues as to the "rewrite loop".)

Make your rules more specific, to match only the URL in the request. For example:

#REWRITE CONDITIONS
RewriteRule ^start$ index.html [L]
RewriteRule ^aboutMe$ about.html [L]
RewriteRule ^contact$ contact.html [L]
RewriteRule ^portfolio$ portfolio.php [L]
RewriteRule ^reportage$ reportage.php [L]

By adding anchors (^ and $) to the RewriteRule pattern it will now only match "portfolio" etc. exactly and also avoid a rewrite loop. The L flag ensures that no further rules will be processed in this pass.

0
0

I changed name of my folders to not conflict RewriteRule 's Ex. Have reportageImages---> my .jpg here now its imagesR - name doesn't matter but it was in conflict someway with that RewriteRule:

RewriteRule reportage reportage.php

Now it works.

5
  • the accepted solution ( by w3dk) is much better. If reading this take his solution :)
    – kolboc
    Commented Jul 21, 2016 at 9:42
  • 1
    Yes, renaming the folders to avoid a conflict is generally undesirable (and avoidable)... change the rules to avoid the conflict instead! :)
    – MrWhite
    Commented Jul 21, 2016 at 9:45
  • kabocweb.ayz.pl can I ask you to take a look, in reportage section it is getting ruined, I checked portfolio.php and it has same code as reportage.php but with different image dir and it's working correctly. Somehow reportage is trying to load everything from unexisting folder and I cannot make out why :I ?
    – kolboc
    Commented Jul 21, 2016 at 14:03
  • I commented on your other comment (before you deleted it?)... you need to remove the <base href="/MG/start"> element from that page (at the end of the head section). All relative links will be seen as relative to the /MG/ folder (which I assume either does not exist anymore, or the document root already points to this subfolder - as mentioned in comments above ...?).
    – MrWhite
    Commented Jul 21, 2016 at 14:07
  • no way i missed that, was even checking because I remembered that I put it there... Sry and thanks, give me address would send beer :)
    – kolboc
    Commented Jul 21, 2016 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.