[英]Apache RewriteCond and RewriteRule to skip for the existing files and directories
我的本地站點具有以下目錄結構。
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
是什么問題和沖突。
在虛擬主機指令中,您需要具有AllowOverride
指令 :
<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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.