2013-07-02 53 views
-1

我想用的.htaccess更改URL,也包括用PHP的.htaccess包括PHP中的文件中獲取變量

舊的頁面:http://website.com?page=test

新:http://website.com/test

<?php include 'pages/'. $_GET['page'] . '.php'; ?> 

我不知道從哪裏開始的.htaccess但任何幫助將是巨大的,謝謝:]

+0

參見['MOD-rewrite'(http://stackoverflow.com/tags/mod-rewrite/信息)的一些例子。詢問你是否有特定的問題,而不是普遍的輔導問題。 – mario

+0

您可能希望將'$ _GET ['page']'中的內容列入白名單,然後將其插入類似的字符串中。 – Mischa

回答

0

試試這個,這個用戶應該看到http://domain.com/thepage和服務器能夠理解的請求作爲http://domain.com/?page=thepage

<IfModule mod_rewrite.c> 
    RewriteEngine On 
    RewriteBase/
    RewriteRule ^([^/]*)$ ?page=$1 [NC,L] 
</IfModule> 
0
<?php include 'pages/'. $_GET['page'] . '.php'; ?> 

單是非常黑客友好。您應該首先將選項限制爲允許的文件。 即在內部點擊/index.php並預設一系列有效選項。也就是說, 取決於文件的數量,預設硬編碼可能是您最好的選擇。

我相信你的意思是mod_rewrite。 http://httpd.apache.org/docs/current/rewrite/remapping.html

增加/.htaccess

<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteBase/
RewriteCond %{QUERY_STRING} ^page=test$ [NC] 
RewriteRule . /test [NC,L,R=301] 
</IfModule> 

或多個動態

<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteBase/
RewriteCond %{QUERY_STRING} ^page=(.*)$ [NC] 
RewriteRule . /%1/ [NC,L,R=301] 
</IfModule> 

我最好的建議是在內部發送一切通過一些PHP解析/驗證,然後的index.php,發送到有效選項的交換機()並且安全地使用默認值:

<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteRule ^index\.php$ - [L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule . /index.php [L] 
</IfModule>