通过 index.php 重新路由所有 php 请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9694118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rerouting all php requests through index.php
提问by firebird
How can I reroute all requests for php pages through index.php?
如何通过 index.php 重新路由对 php 页面的所有请求?
My .htaccess is as follows:
我的 .htaccess 如下:
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]
Basically, I'm trying to emulate .NET master pages. The index.php has the site header/footer.
基本上,我正在尝试模拟 .NET 母版页。index.php 具有站点页眉/页脚。
It redirects 404 to index.php. How can I make it redirect all requests to php pages (except index.php itself)?
它将 404 重定向到 index.php。如何让它将所有请求重定向到 php 页面(index.php 本身除外)?
Is there any performance issues with this method (don't want to use a framework)?
这种方法是否有任何性能问题(不想使用框架)?
回答by powerbuoy
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/?(.*)$ [R=301,L]
# Run everything else but real files through index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/?%{QUERY_STRING} [L]
</IfModule>
As the comments suggest it will route every request that isn't an actual file to index.php
正如评论所暗示的那样,它会将不是实际文件的每个请求路由到 index.php
回答by user1149440
Use:
用:
RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond !^(index\.php|public|css|js|robots\.txt) RewriteRule ^(.*)$ index.php/params= [L,QSA] ErrorDocument 404 /index.php
Url sample: www.site.com/friend/josh/03-13-2012
网址示例:www.site.com/friend/josh/03-13-2012
So the $params var value:
所以 $params var 值:
friend/josh/03-13-2012
朋友/乔希/03-13-2012
Only need explode() "/" so u get array with params:
只需要explode() "/" 这样你就可以得到带有参数的数组:
array(
0 => friend
1 => josh
2 => 03-13-2012
)
回答by bkconrad
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !index\.php$
RewriteRule .*\.php$ index.php?q=%{REQUEST_URI} [L]
Will catch all requests ending in .php that don't point to a file ending in index.php and redirect them to index.php in the get parameter q
将捕获所有以 .php 结尾的请求,这些请求不指向以 index.php 结尾的文件,并将它们重定向到 get 参数中的 index.php q
回答by starkeen
To redirect all non-existent requests to /index.php, You can also use ErrorDocumentand FallbackResourcedirectives .
要将所有不存在的请求重定向到/index.php,您还可以使用ErrorDocument和FallbackResource指令。
Try one of the following examples :
尝试以下示例之一:
ErrorDocument 404 /index.php
FallbackResource /index.php
These directives are part of apache core modules and you dont need to enable mod-rewrite to use them.
这些指令是 apache 核心模块的一部分,您不需要启用 mod-rewrite 来使用它们。
If you are looking for a way to redirect all requests (including real files/dirs) then try the following Redirect
如果您正在寻找一种重定向所有请求(包括真实文件/目录)的方法,请尝试以下重定向
RedirectMatch ^/(?!index\.php).+)$ /index.php