You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

配置.htaccess Rewrite规则后本地图片无法显示求助

Fixing Image Display Issues After .htaccess Rewrite Rules

Hey there! I totally get how frustrating it is when SEO URL rewrites break your image loading—let's get this sorted out quickly.

The Root Cause

When you use rewrite rules to turn page.php?page_alias=some-page into page/some-page/, browsers get confused about the current directory path. If your images use relative paths like images/photo.jpg, the browser will try to load them from page/some-page/images/photo.jpg instead of the actual root images/ folder. That's why they're not showing up!

3 Simple Fixes

  • Use absolute paths for all assets
    Update every image, CSS, and JS reference to start with a / (root directory). For example:

    <img src="/images/your-image.png" alt="Description">
    <link rel="stylesheet" href="/css/style.css">
    

    This tells the browser to always look for assets from the root, no matter what the URL looks like.

  • Add a <base> tag to your HTML head
    Stick this line inside the <head> section of your page template:

    <base href="/">
    

    This sets a global base URL for all relative paths on the page, so the browser knows to resolve them from the root directory instead of the rewritten URL's "fake" subdirectory.

  • Modify your .htaccess to exclude static files
    If you don't want to touch your HTML code, update your rewrite rules to skip existing files and directories (like images, CSS, etc.). Here's how:

    RewriteEngine On
    
    # Skip existing files/directories so they load normally
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]
    
    # Your original SEO rewrite rules (added [L] flag to stop processing after match)
    RewriteRule ^page/([a-zA-Z0-9-/]+)$ page.php?page_alias=$1 [L]
    RewriteRule ^page/([a-zA-Z0-9-/]+)/$ page.php?page_alias=$1 [L]
    

    The [L] flag ensures that once a rule matches, no further rules are applied. The first two conditions tell Apache to ignore any request that's for an actual file or directory.

Any of these methods should fix your image display issue. The first two are my go-to picks since they're straightforward and prevent similar path issues in the future.

内容的提问来源于stack exchange,提问作者Deepak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:43:18