PHP 将所有路径重定向到某个 php 文件? [英] PHP redirecting all paths to a certain php file?
问题描述
假设我的网站中有一个名为test"的主文件夹,其中包含一个 index.php 文件www.example.com/test/index.php
Let's say I have a main folder in my website named "test" which contains an index.php file www.example.com/test/index.php
如何才能使包含此路径www.example.com/test"的任何 url 重定向到www.example.com/test/index.php"并仍保留第一个请求路径
How can I make it so that any url which contains this path "www.example.com/test" redirects to "www.example.com/test/index.php" and still hold the first request path
例如:
www.example.com/test/User redirects to www.example.com/test/index.php
www.example.com/test/User/2 redirects to www.example.com/test/index.php
www.example.com/test/Account redirects to www.example.com/test/index.php
www.example.com/test/Account/5 redirects to www.example.com/test/index.php
推荐答案
使用 Apache 的 重写模块.例如,在虚拟主机配置或目录的 .htaccess
文件中,您可以这样写:
Use Apache's Rewrite module. For example, in a virtual-hosts config, or in a directory's .htaccess
file, you might write this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
这会使任何接近该目录的 URL 重定向到 index.php
,但它不会更改脚本解析的 URL.
This would make any URL that approached that directory redirect to index.php
but it would not change the URL the script parses.
以下是一些关于在托管环境中使用重写的 Godaddy.com 文档:
Here are some Godaddy.com docs on using rewrites in their hosting environment:
这篇关于PHP 将所有路径重定向到某个 php 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!