2016-10-30 177 views
0

我需要在我的網址中有1個變量或有時2個變量。重寫mod無法正常工作

http://example.com/home 
or http://example.com/home/hr 

我創建了以下.htaccess文件夾public_html

RewriteEngine On RewriteRule ^([a-zA-Z0-9]+)$ index.php?page=$1 
RewriteRule ^([a-zA-Z0-9]+)/$ index.php?page=$1 ReWriteRule 
^([^/]+)/(\d+)/? index.php?page=$1&q=$2 

它不工作!我確實有public_htmlhr.phpindex.phppublic_html/view

現在hr.php包括在index.php

include("view/hr.php") 

所以現在index.php有頁面變量,/view/hr.php有第二個變量。

我該如何讓它工作?

回答

0

不要把你的邏輯放在.htaccess之內,把除了你的靜態文件之外的所有東西都改寫成一個php文件,然後在裏面路由你的請求。例如: -

# only rewrite static file URLs when not found, otherwise let them through 
RewriteCond $0 !^(?:js|css|img|video|audio)$ [OR] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond ^[^/]+ index.php 

現在,在index.php,你有$_SERVER['REQUEST_URI']網址,你們可以包括任何你想要的文件。

+0

它沒有爲我工作! :( – user2305672

+0

什麼是不工作?是否調用了'index.php'?你確定'RewriteEngine'是'On'嗎?你檢查了$ _SERVER ['REQUEST_URI']'的值嗎? – Walf