我的本地网站有以下目录结构。
mysite
|_ css
|_ images
|_ logo.png
|_ js
|_ app
|_ index.php
|_ .htaccess
我正在尝试将Apache URL重写为重定向到http://mysite.dev/app/index.php
以查找不存在的目录。
例如,http://mysite.dev/en/home
将被重写为http://mysite.dev/app/index.php
我在.htaccess
文件中尝试了这个重写规则:
Options -Indexes
DirectoryIndex index.php index.html index.htm
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ app/index.php [L]
</IfModule>
但是它被重写为所有现有目录和没有现有目录。
也就是说,当我浏览http://mysite.dev/mysite/images/logo.png
时,它会被重写为http://mysite.dev/mysite/app/index.php
我在两条RewriteCond
行上方添加了一行,但它没有用。
RewriteRule ^(images|css|js) - [NC,L]
如果存在任何文件或目录,我的目标是跳过规则。
如何为此目的撰写RewriteCond
和RewriteRule
?
[编辑]
我发现了这个问题。这是因为我为我的网站添加了虚拟主机。我在httpd-vhost.conf
中有以下几行代码:
<VirtualHost mysite.dev>
DocumentRoot "C:/xampp/htdocs/mysite"
ServerName mysite.dev
ServerAlias mysite.dev
<Directory "C:/xampp/htdocs/mysite">
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
当我浏览http://mysite.dev/images/logo.png
时,RewriteCond
无法正常工作
但当我删除虚拟主机并浏览http://localhost/mysite/images/logo.png
时,它工作正常,我正确地看到了图像。
我不确定虚拟主机和RewriteCond
的问题和冲突是什么。
答案 0 :(得分:0)
在您的虚拟主机指令中,您需要AllowOverride
directive:
<VirtualHost mysite.dev>
DocumentRoot "C:/xampp/htdocs/mysite"
ServerName mysite.dev
ServerAlias mysite.dev
<Directory "C:/xampp/htdocs/mysite">
AllowOverride FileInfo
Order allow,deny
Allow from all
</Directory>
</VirtualHost>