htaccess用参数重写规则

时间:2015-01-24 00:16:18

标签: apache .htaccess mod-rewrite

我的服务器根目录中有以下文件:

/index.php
/css/file.css
/js/file.js

我想重定向:

  • http://host/entry/1http://host/index.php?entry=1

到目前为止,我有以下.htaccess

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-s
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} /entry/ [NC]
    RewriteRule ^entry/(.*)$ index.php?entry=$1 [QSA,NC,L]

</IfModule>

问题是cssjs文件未正确投放。

这是因为浏览器要求提供文件:

  • http://host/entry/css/file.css代替http://host/css/file.css
  • http://host/entry/js/file.js代替http://host/js/file.js

我该如何解决?

由于

1 个答案:

答案 0 :(得分:1)

使用:

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} -d [OR]
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -s
    RewriteRule ^ - [L]

    RewriteRule ^entry/(.+\.(?:js|css))$ $1 [NC,L]

    RewriteRule ^/content/ag(.*)$ index.php?entry=$1 [QSA,NC,L]
    RewriteRule ^entry/(.*)$ index.php?entry=$1 [QSA,NC,L]
</IfModule>

并添加你的html标题:

<base href="/">

我为.js和.css添加重写 但是你可以添加其他内容,例如.gif或.png ...