.htaccess只为页面重写url,而不是目录

时间:2012-09-05 21:41:25

标签: .htaccess

现在我有一些htaccess规则要将我的url从example.com?node=home重新编写为example.com/home,将example.com?node=gallery&id=56重写为exmaple.com/gallery/56。它还修复了我的链接目录路径,css样式表,照片等网页本身正确链接,现在所有这一切都正常工作,我遇到的问题是如果我尝试访问说,example.com /filename.jpg,它认为filename.jpg是一个应该尝试加载的页面,而不是将我带到目录。那么怎么会这样,我怎么能解决这个问题,并且我的htaccess规则仍然可以正常工作呢?

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
## don't do anything
RewriteRule ^ - [L]

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

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

现在我也在index.php页面上:

<base href="http://www.mysite.com">

1 个答案:

答案 0 :(得分:0)

尝试将代码更改为:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^([^/]+)/([^/]+)/?$ index.php/?node=$1&id=$2 [L,QSA]

## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^([^/]+)/?$ index.php/?node=$1 [L,QSA]