配置.htaccess Rewrite规则后本地图片无法显示求助
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

