简体   繁体   English

RewriteRule匹配所有字符

[英]RewriteRule to match all characters

So, I'm trying to write a htaccess file that will rewrite everything back to the root index file with a query added after it, so for example 因此,我正在尝试编写一个htaccess文件,该文件将使用在其后添加的查询将所有内容重写回根索引文件。

example.com/cheese/ will route to example.com/index.php?cheese/ example.com/cheese/将路由到example.com/index.php?cheese/

example.com/cheese will route to example.com/index.php?cheese example.com/cheese将路由到example.com/index.php?cheese

example.com/bob/fred will orute to example.com/index.php?bob/fred example.com/bob/fred将转为example.com/index.php?bob/fred

going in as many layers as necessary, does anyone have an suggestions? 进入尽可能多的层次,有人有建议吗?

Currently trying this but it isn't working: 目前正在尝试此操作,但无法正常工作:

RewriteEngine on
RewriteRule ^(.*)/?$ index.php?$1

I know it isn't working because I am testing it with the following and the query output is not correct. 我知道它不起作用,因为我正在使用以下命令对其进行测试,并且查询输出不正确。

<?php
    $page = $_SERVER['QUERY_STRING'];
    echo $page;
?>

You can use this rule in your DOCUMENT_ROOT/.htaccess file: 您可以在DOCUMENT_ROOT/.htaccess文件中使用此规则:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php?$1 [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f will avoid looping and skip valid files like css, js images being rewritten to index.php . RewriteCond %{REQUEST_FILENAME} !-f将避免循环,并跳过有效的文件(如css,js图像)重写为index.php

Access the query string as: 通过以下方式访问查询字符串:

$_SERVER["QUERY_STRING"]

At first, mod_rewrite must be enabled. 首先,必须启用mod_rewrite

<IfModule mod_rewrite.c>
      RewriteEngine On
      RewriteBase /
      RewriteRule /(.*) index.php?$1
</IfModule>

But if using $_SERVER['QUERY_STRING'] is a necessity I'd suggest using: 但是,如果需要使用$_SERVER['QUERY_STRING']我建议您使用:

<IfModule mod_rewrite.c>
      RewriteEngine On
      RewriteBase /
      RewriteRule . index.php
</IfModule>

and afterwards retrieve url from $_SERVER['REQUEST_URI'] , because your solution breaks every GET request. 然后从$_SERVER['REQUEST_URI']检索url,因为您的解决方案中断了每个GET请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM