2016-10-30 127 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