网址重写js,css等问题

时间:2017-04-25 12:00:34

标签: javascript css apache mod-rewrite

在我的本地apache上运行的.htaccess中,我的URL重写有点问题。我通过符号链接(localhost / memap /)配置了服务器的入口点,因此“memap”指向我的项目根目录(没有配置vhost)。

我对url重写不太熟悉,但将所有请求重定向到我的index.php工作正常。 index.php指向一个自定义请求控制器,它处理请求并返回相应的页面。

我现在的问题是没有js,css等被加载。我尝试了几个RewriteRules,但没有得到解决方案。这是我当前的.htaccess文件,它不包含任何js,css等规则:

# Allows ModRewrite to work
Options FollowSymLinks

# Turn on rewrite engine
RewriteEngine On
RewriteBase /memap

# Redirect all requests to index
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

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

如果我没有弄错的话,重写条件应该在目标文件/目录存在的情况下将文件和目录的请求直接发送到目标,而不进行实际重写。

不幸的是,这种情况并没有发生。例如。我的css文件(memap.css)可以通过“localhost / memap / css / memap.css”访问,但服务器查找“localhost / css / memap.css”(没有我的memap符号链接)。因此,我的js(“localhost / js / memap.js”)也会发生同样的情况。这是如何将它们包含在我的html中:

<script src="js/memap.js"></script>
<link rel="stylesheet" href="css/memap.css" media="screen" />

我尝试了很多东西,但不知道如何实现我的目标。有人有解决方案吗?这可能只是一个遗漏的小事......

提前致谢!

1 个答案:

答案 0 :(得分:0)

您的RewriteCond仅适用于第一个RewriteRule

你可以写两次RewriteCond块:

RewriteBase /memap/

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

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

或者您可以使用RewriteRule跳过两个[S=2]

RewriteBase /memap/

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [S=2]
RewriteRule ^page/(.*)/(.*)$ index.php?page=$1&id=$2 [NC,L,QSA]
RewriteRule ^page/(.*)$ index.php?page=$1 [NC,L,QSA]

你应该为你的JS&amp; CSS文件:

<script src="/memap/js/memap.js"></script>
<link rel="stylesheet" href="/memap/css/memap.css">