Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
398 views
in Technique[技术] by (71.8m points)

.htaccess - Rerouting all php requests through index.php

How can I reroute all requests for php pages through index.php?

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]

Basically, I'm trying to emulate .NET master pages. The index.php has the site header/footer.

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)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

As the comments suggest it will route every request that isn't an actual file to index.php


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...