将每个查询重定向到上层文件夹中的文件
Redirect every query to file in upper folder
我的网络服务器上有这个结构:
\main
index.php
index.php
文件可通过 www.example.com/main/
访问。
现在我想将每个具有子文件夹结构(不存在的文件夹)的查询重定向到我的 index.php
文件并传递输入的 URL.
喜欢:
www.example.com/main/world/fish
www.example.com/main/hasuisdg
www.example.com/main/bowl/soup
www.example.com/main/salat
所有这些 URL 应该由我的 index.php
在通过 URL 时回答。
一般示例:
index.php
代码:
<?php
echo passedurl(); //this is the function I am looking for
?>
正在浏览器中输入 www.example.com/main/ilikeWhosebug
。
在网页上显示 www.example.com/main/ilikeWhosebug
。
不是绝对重要:
URL 更改为 www.example.com/main/
。
Not absolutely important: The url changes to www.example.com/main/
除了最后一点(如果这确实是一个 可选要求 或 不需要的行为?)。如果所有 URLs "changed to" /main/
那么您就不能轻松地适当地路由请求 - 不可能添加书签 - 并且会使用户感到困惑。
您没有说明 您希望 .htaccess
文件去哪里。我假设您希望它位于您网站的根目录中。 (您也可以将它放在 /main
子目录中 - 但您需要稍微更改指令。)
例如,使用 mod_rewrite 在内部将 URL /main/<something>
的请求重写为 /main/index.php
,其中 /main/<something>
不映射到目录,尝试以下操作:
RewriteEngine On
RewriteBase /main
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^main/[^.]+$ index.php [L]
正则表达式 ^main[^.]+$
匹配不包含点的 URL - 因此这自然排除了看起来像文件的请求。因此,我排除了对现有文件的检查(仅针对现有目录 - 如上所述)。
然后,在 index.php
中,您可以检查 $_SERVER['REQUEST_URI']
超全局以访问请求的 URL。不一定需要将请求的 URL 显式传递给您的脚本。请注意,PHP 变量 $_SERVER['REQUEST_URI']
还包含查询字符串(如果有)。 (RewriteRule
模式 仅匹配 URL 路径,这明显排除了查询字符串。)
我的网络服务器上有这个结构:
\main
index.php
index.php
文件可通过 www.example.com/main/
访问。
现在我想将每个具有子文件夹结构(不存在的文件夹)的查询重定向到我的 index.php
文件并传递输入的 URL.
喜欢:
www.example.com/main/world/fish
www.example.com/main/hasuisdg
www.example.com/main/bowl/soup
www.example.com/main/salat
所有这些 URL 应该由我的 index.php
在通过 URL 时回答。
一般示例:
index.php
代码:
<?php
echo passedurl(); //this is the function I am looking for
?>
正在浏览器中输入 www.example.com/main/ilikeWhosebug
。
在网页上显示 www.example.com/main/ilikeWhosebug
。
不是绝对重要:
URL 更改为 www.example.com/main/
。
Not absolutely important: The url changes to
www.example.com/main/
除了最后一点(如果这确实是一个 可选要求 或 不需要的行为?)。如果所有 URLs "changed to" /main/
那么您就不能轻松地适当地路由请求 - 不可能添加书签 - 并且会使用户感到困惑。
您没有说明 您希望 .htaccess
文件去哪里。我假设您希望它位于您网站的根目录中。 (您也可以将它放在 /main
子目录中 - 但您需要稍微更改指令。)
例如,使用 mod_rewrite 在内部将 URL /main/<something>
的请求重写为 /main/index.php
,其中 /main/<something>
不映射到目录,尝试以下操作:
RewriteEngine On
RewriteBase /main
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^main/[^.]+$ index.php [L]
正则表达式 ^main[^.]+$
匹配不包含点的 URL - 因此这自然排除了看起来像文件的请求。因此,我排除了对现有文件的检查(仅针对现有目录 - 如上所述)。
然后,在 index.php
中,您可以检查 $_SERVER['REQUEST_URI']
超全局以访问请求的 URL。不一定需要将请求的 URL 显式传递给您的脚本。请注意,PHP 变量 $_SERVER['REQUEST_URI']
还包含查询字符串(如果有)。 (RewriteRule
模式 仅匹配 URL 路径,这明显排除了查询字符串。)