2014-08-29 50 views
2

我一直在研究一個我在php中創建的框架,它的基本部分是發送所有的url查詢到index.php文件夾中。 我設法在我的.htaccess文件.htaccess重定向到任何目錄中的index.php

# Turn rewriting on 
Options +FollowSymLinks 
RewriteEngine On 
# Redirect requests to index.php 
#RewriteRule ^(phpmyadmin)($|/) - [L] 
RewriteRule ^(html)($|/) - [L] 
RewriteCond %{REQUEST_URI} !=/index.php 
RewriteCond %{REQUEST_URI} !.*\.png$ [NC] 
RewriteCond %{REQUEST_URI} !.*\.jpg$ [NC] 
RewriteCond %{REQUEST_URI} !.*\.css$ [NC] 
RewriteCond %{REQUEST_URI} !.*\.gif$ [NC] 
RewriteCond %{REQUEST_URI} !.*\.js$ [NC] 
RewriteRule .* /index.php 

做在folloowing代碼時,我的網站移動到不同的文件夾,例如

www.mydomain.com/subdir/{where the index.php is, along with htaccess} 

,並停止在這裏工作出現問題。如果我將框架應用程序移動到子域,也會發生同樣的情況。

所以我試圖修改.htaccess來正確地重寫index.php在.htaccess文件所在的位置,而不管它是否在子目錄或子域中。我怎樣才能讓.htaccess知道應用程序在子目錄中並正確地重寫了它,所以位置改變不會中斷指向正確文件的.htaccess?

www.domain.com/{.htaccess+index.php + framework} => works properly 
www.subdom.domain.com/{.htaccess+index.php + framework} => does not work 
www.domain.com/subdir/{.htaccess+index.php + framework} => does not work //send to www.domain.com/index.php 
localhost/projectname/{.htaccess+index.php + framework} =>does not work 

正如你所看到的,它需要發送請求到index.php,其中htaccess也位於。

+0

變化'/ index.php'只是'index.php'?當然現在你需要在EVERY目錄中有一個index.php。 – 2014-08-29 18:27:29

+1

正在使用'RewriteBase'不是你的選擇嗎?有了它,當需要移動時需要移動時,可以通過在「.htaccess」中更改2個條目來輕鬆解決問題。 'RewriteBase/folder_it_is_now /'和你的'/ index.php'到'RewriteRule'的'index.php'中。 – Prix 2014-08-29 18:58:38

+0

@Prix你能提供任何樣品嗎?我不是很瞭解。 – Justin 2014-08-29 19:01:32

回答

3

您可以使用這一招總是在當前目錄下的index.php文件寫:

RewriteEngine On 

# Generate BASE dynamically 
# It compares REQUEST_URI variable (which is complete path) with the URI matched by 
# RewriteRule (which is relative to current path) and gets differential in 
$ %{ENV:BASE} variable. 
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$ 
RewriteRule ^(.*)$ - [E=BASE:%1] 

RewriteRule ^(html)($|/) - [L] 

# Redirect requests to %{ENV:BASE}index.php which is index.php of current directory 
# It also makes sure index.php exists in current directory 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{DOCUMENT_ROOT}%{ENV:BASE}index\.php -f [NC] 
RewriteRule . %{ENV:BASE}index.php [L] 
+1

+1非常有趣的'ENV',你不需要根據'%{DOCUMENT_ROOT}'檢查它是否存在'index.php'嗎? – Prix 2014-08-29 19:09:24

+0

@anubhava我剛剛在我的本地主機上測試了tghis,除非我註釋掉「Options + FollowSymLinks」,否則它不會工作。它究竟扮演什麼角色? – Justin 2014-08-29 19:13:48

+0

'+ FollowSymLinks'表示'服務器將遵循此目錄中的符號鏈接.'它只會影響'DocumentRoot'是否使用某個符號鏈接。 – anubhava 2014-08-29 19:49:33

2

根據新的例子編輯:

這樣的事情呢?基本上檢查,以確保它不是一個文件,如果沒有一個文件重定向到index.php

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond $1 !^(index\.php) 
RewriteRule ^(.+)$ index.php [PT,L,QSA] 
+0

我看到這樣可以解決這個問題,但我試圖用.htaccess搞清楚它本身。 – Justin 2014-08-29 18:33:15

+0

除非你有其他特殊情況,否則apache不會知道新的subdir在哪裏。你有沒有嘗試使用的URL的例子?他們是否遵循特殊模式? – Chris 2014-08-29 18:39:33

+0

我剛剛添加了示例網址供您參考。請看一看。 – Justin 2014-08-29 18:46:28

相關問題