[英]RewriteRule to match all characters
因此,我正在尝试编写一个htaccess文件,该文件将使用在其后添加的查询将所有内容重写回根索引文件。
example.com/cheese/
将路由到example.com/index.php?cheese/
example.com/cheese
将路由到example.com/index.php?cheese
example.com/bob/fred
将转为example.com/index.php?bob/fred
进入尽可能多的层次,有人有建议吗?
目前正在尝试此操作,但无法正常工作:
RewriteEngine on
RewriteRule ^(.*)/?$ index.php?$1
我知道它不起作用,因为我正在使用以下命令对其进行测试,并且查询输出不正确。
<?php
$page = $_SERVER['QUERY_STRING'];
echo $page;
?>
您可以在DOCUMENT_ROOT/.htaccess
文件中使用此规则:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php?$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
将避免循环,并跳过有效的文件(如css,js图像)重写为index.php
。
通过以下方式访问查询字符串:
$_SERVER["QUERY_STRING"]
首先,必须启用mod_rewrite
。
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule /(.*) index.php?$1
</IfModule>
但是,如果需要使用$_SERVER['QUERY_STRING']
我建议您使用:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule . index.php
</IfModule>
然后从$_SERVER['REQUEST_URI']
检索url,因为您的解决方案中断了每个GET请求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.