2017-04-12 90 views
0

對於域www.example.com,我希望將用戶引導至index.php。使用只在特定情況下試圖通過htaccess重寫網址

DirectoryIndex index.php 

我這樣做。但是,如果他們有一個斜線和非文件類型的值,我想將它們重定向到一個不同的頁面,並使用該值作爲一個PHP變量得到:

www.example.com/hello lands on a_different_page.php?var=hello 

但我不想打擾以「php」結尾的請求例如:

www.example.com/page.php 

不應嘗試重定向。同時還爲普通的URL的請求不應得到改變:

www.example.com goes to index.php 

我已經找到辦法,使其中的一些發生,但他們總是與其他請求非預期的後果干擾。任何幫助將不勝感激 - 我對htaccess重寫的研究沒有幫助。

回答

0

您可以使用下面的htaccess:

DirectoryIndex index.php 
RewriteEngine on 
#rewrite /foobar unless a real file and dir to /page.php?var=foobar 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule (.+) /page.php?var=$1 [L] 
+0

訣竅!謝謝。 – Jim

0

處理這個問題的最好方法是不重定向任何指向已存在的東西。

RewriteCond %{REQUEST_FILENAME} -f 
RewriteRule (.*) a_different_page.php?var=$1 
0

希望它會正常工作。對我來說工作完全沒問題。

RewriteEngine On 

Options -Multiviews 
#For rewriting/request to index.php 
RewriteCond %{REQUEST_URI} ^/?$ 
RewriteRule ^(.*)$ /index.php [L] 

#For redirecting /some/anonymous/request to some_different_page.php?var=/some/anonymous/request when request uri does not ends with .php 
RewriteCond %{REQUEST_URI} !\.(php)$ 
RewriteRule ^(.*)$ /some_different_page.php?var=$1 [R=301,L,QSA]