php - htaccess 重定向隐藏 php 扩展或传递参数

标签 php apache .htaccess mod-rewrite redirect

我正在使用主 index.php 文件来处理大部分请求,所以我的基本 .htacess 看起来像 this one :

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php?page=$1 [L]

但我有一些请求(如/signin 或/signout)需要直接从同名的 .php 文件(/signin.php 和/signout.php)处理。

所以我的 .htaccess 规则需要是:“if %{REQUEST_FILENAME}.php does not exists redirect to /index.php?page=$1 else redirect to %{REQUEST_FILENAME}.php”。

我该怎么做?

最佳答案

用这个替换你的代码:

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L,NC]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php !-f
RewriteRule ^(.+)$ /index.php?page=$1 [L,QSA]

这会将不是有效文件或目录且在您的文件系统中不存在匹配的 .php 的所有内容发送到 /index.php

关于php - htaccess 重定向隐藏 php 扩展或传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19223226/

相关文章:

ruby-on-rails - 在 Rails(本地)开发环境中并排运行 HTTP 和 HTTPS

angularjs - 在 tomcat apache 服务器上部署 AngularJS 应用程序

php - 程序化php/mysql到面向对象的问题

php - 如果内存过度使用,哪个进程会被内核杀死? child 还是 parent ?

php - Wordpress 仪表板空白屏幕

php - 如何从 Composer 安装 CakePHP 插件到 app/Plugin

php - 如何删除网址中的问号

apache - HTACCESS 重定向

php - Apache/.htaccess - 将所有请求重定向到单个页面,但该页面使用的 Assets 除外

.htaccess 重定向到 https,除了一页