大网数据广告活动
网站运营与故障问答

.htaccess image localhost

· 52CE 我爱测

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 RewriteEngin…

问题原文

原帖链接 · kolboc · CC BY-SA 3.0

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:

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

已采纳解答

原帖链接 · MrWhite · CC BY-SA 3.0

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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2016-07-20)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。