我正在devise我的应用程序。 我应该做下一件事。 所有的GET参数(?var = value)在mod_rewrite的帮助下都应该被转换成/ var / value。 我怎样才能做到这一点? 我只有1个.php文件(index.php),因为我正在使用FrontController模式。 你能帮我这个mod_rewrite规则?
对不起我的英语不好。 先谢谢你。
我在使用“seo友好”网址的网站上做这样的事情。
在.htaccess中:
Options +FollowSymLinks RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule .* /index.php [L]
然后在index.php上:
if ($_SERVER['REQUEST_URI']=="/home") { include ("home.php"); }
.htaccess规则告诉它如果找不到文件或目录,则加载index.php。 然后你只是解析请求URI来决定index.php应该做什么。
下面的.htaccess中的代码将重写您的URL从例如。 /api?other=parameters&added=true
to /?api=true&other=parameters&added=true
RewriteRule ^api/ /index.php?api=true&%{QUERY_STRING} [L]
的.htaccess
RewriteEngine On # generic: ?var=value # you can retrieve /something by looking at $_GET['something'] RewriteRule ^(.+)$ /?var=$1 # but depending on your current links, you might # need to map everything out. Examples: # /users/1 # to: ?p=users&userId=1 RewriteRule ^users/([0-9]+)$ /?p=users&userId=$1 # /articles/123/asc # to: ?p=articles&show=123&sort=asc RewriteRule ^articles/([0-9]+)/(asc|desc)$ /?p=articles&show=$1&sort=$2 # you can add /? at the end to make a trailing slash work as well: # /something or /something/ # to: ?var=something RewriteRule ^(.+)/?$ /?var=$1
第一部分是收到的URL。 第二部分重写的URL,你可以使用$_GET
读出。 (
和)
之间的所有内容都被视为一个变量。 第一个将是$1
,第二个$2
。 这样,您就可以确定变量在重写的URL中的确切位置,从而知道如何检索它们。
你可以保持它非常一般,通过使用(.+)
来允许“一切”。 这只是表示:一个或多个( +
)的任何字符( .
)。 或者更具体,例如只允许数字: [0-9]+
(0到9范围内的一个或多个字符)。 你可以在http://www.regular-expressions.info/找到关于正则表达式的更多信息。 这是一个很好的网站来测试他们: http : //gskinner.com/RegExr/ 。
AFAIK mod_rewrite在问号之后不处理参数 – 重写规则的regexp行尾匹配“?”之前的路径末尾。 所以,你几乎只限于传递参数,或者在重写的时候完全放弃参数。