通过 index.php 重新路由所有 php 请求 [英] Rerouting all php requests through index.php
问题描述
如何通过 index.php 重新路由对 php 页面的所有请求?
How can I reroute all requests for php pages through index.php?
我的.htaccess如下:
My .htaccess is as follows:
Options +FollowSymLinks
IndexIgnore */*
#Turn on the RewriteEngine
RewriteEngine On
# Rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !.*.(js|ico|gif|jpg|png|css|pdf)$ index.php%{REQUEST_URI} [L]
基本上,我正在尝试模拟 .NET 母版页.index.php 包含站点页眉/页脚.
Basically, I'm trying to emulate .NET master pages. The index.php has the site header/footer.
它将 404 重定向到 index.php.如何让它将所有请求重定向到 php 页面(index.php 本身除外)?
It redirects 404 to index.php. How can I make it redirect all requests to php pages (except index.php itself)?
这种方法是否有任何性能问题(不想使用框架)?
Is there any performance issues with this method (don't want to use a framework)?
推荐答案
这是我使用的(并且已经使用了很长时间):
Here's what I use (and have used for ages):
<IfModule mod_rewrite.c>
# Redirect /index.php to / (optional, but recommended I guess)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /.*index.php
RewriteRule ^index.php/?(.*)$ $1 [R=301,L]
# Run everything else but real files through index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1?%{QUERY_STRING} [L]
</IfModule>
正如评论所暗示的那样,它会将每个不是实际文件的请求路由到 index.php
As the comments suggest it will route every request that isn't an actual file to index.php
这篇关于通过 index.php 重新路由所有 php 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!