2016-04-26 103 views
1

我試圖做一個雙重定向並不能得到正確的邏輯:雙的.htaccess規則

如果頁面存在,忽略所有規則

2.如果頁面不存在,添加.php擴展名(但將其隱藏在URL欄中)

3.如果該頁面不存在並且它不存在且.php擴展名重定向到單個頁面頁面(query.php)並將輸入的內容追加到url欄中作爲一個可變參數(並在URL欄中輸入)。

實施例的文件結構:

的index.php
pricing.php
query.php

實施例試驗:

[domain]/pricing.php -> [domain]/pricing (showing the pricing.php page) 
[domain]/pricing  -> [domain]/pricing (showing the pricing.php page) 
[domain]/somethingelse -> [domain]/somethingelse (showing the query.php page with ?somethingelse appended) 

的.htaccess

RewriteEngine on 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-l 
RewriteRule ^(.*)$ $1.php 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-l 
RewriteRule ^(.*)$ query.php?$1 [L] 

實際測試:

[domain]/pricing.php -> [domain]/pricing (showing the pricing.php page) 
[domain]/pricing  -> [domain]/pricing (showing the pricing.php page) 

[domain]/somethingelse -> [domain]/somethingelse (showing the query.php page with ?somethingelse_php appended) 

.htaccess文件上面我已經做的一切我想重定向到query.php它時,它除了當我吐出$ _GET變量顯示「_php」追加。我假設因爲早先的規則...

+0

Rockett's answ呃很好。以下是確保/路由到index.php的額外步驟:放置在其他規則頂部: #重新路由/到index.php RewriteCond%{REQUEST_URI} ^/$ RewriteRule ^(。*)$/index.php [L] – jasondeegan

回答

0

請嘗試以下代替。這些條件/規則集特定於您列出的要求,併爲您提供對用戶服務的細粒度控制。

RewriteEngine On 

# Serve the requested file if it exists 
RewriteCond %{REQUEST_FILENAME} -f 
RewriteRule^- [L] 

# If not, then check if its .php equivalent exists 
RewriteCond %{REQUEST_FILENAME}.php -f 
RewriteRule (.+)/?$ $1.php [L] 

# Otherwise, send the request off to query.php 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule (.*)/?$ /query.php?$1 [L] 

在過去的規則方面,要求/testing應導致的轉儲在query.php如果/testing.php不存在使用var_sump($_GET)時:

array(1) { ["testing"]=> string(0) "" } 
+0

謝謝 - 根據我的要求,這是正確的,因此標記正確。我沒有要求它確保[域名] /顯示索引頁面而不是查詢頁面...我會指出這一部分。 – jasondeegan

0

試試下面的代碼

RewriteEngine On 
RewriteBase/
ErrorDocument 404 http://path/to/query.php 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-l