img不适用于htaccess重定向

时间:2014-11-04 06:58:19

标签: php apache .htaccess mod-rewrite redirect

我有一个代码来重定向这样的链接

RewriteEngine On  
Options +FollowSymLinks  

RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d  

RewriteRule ^([^/\.]+)/([^/\.]+)?$ pages/index.php?p=$1&page=$2 [L]  

RewriteRule ^([^/\.]+)?$ pages/index.php?p=$1 [L]

这样

localhost/mysite/a
localhost/mysite/a/b

转到

localhost/mysite/pages/index.php?p=a
localhost/mysite/pages/index.php?p=a&page=b

分别

我在index.php中有

的代码
<img src='../pages/images/12345h1.png'>

img适用于localhost / mysite / a / b链接,但它不适用于localhost / mysite / a 有没有办法解决这个问题?

<{1}}和localhost/mysite/a/b

src变为localhost/mysite/a/ 但对于localhost/mysite/pages/images/12345h1.png src变为localhost/mysite/a

1 个答案:

答案 0 :(得分:0)

问题是你的RewriteCond只能应用于下一个立即的`RewriteRule。像这样更改.htaccess:

RewriteEngine On  
Options +FollowSymLinks  

# skip rules for files and directories    
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d  
RewriteRule ^ - [L]

RewriteRule ^([^/.]+)/([^/.]+)/?$ pages/index.php?p=$1&page=$2 [L,QSA] 

RewriteRule ^([^/.]+)/?$ pages/index.php?p=$1 [L,QSA]