使用htaccess

时间:2015-12-23 19:21:53

标签: php apache .htaccess

对于网络应用程序,我想重写网址。

自:

http://example.com/api/logger/all

要:

http://example.com/api/index.php/logger/all

所以我尝试了以下内容:

RewriteEngine On
RewriteCond %{REQUEST_URI} !/index.php/  
RewriteRule ^api(.*)$ api/index.php$1 [L]

我也用http://htaccess.madewithlove.be/成功测试了它。

在服务器上我得到了404。

我的项目结构如下所示:

--api
----index.php
--htaccess
--index.html

更新

我找到了外部重定向的解决方案。

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !(index\.php)
RewriteRule ^api/(.*)$ /api/index.php/$1 [R=302]

但是我需要一个内部重定向,如果删除[R = 302]我得到404"没有指定输入文件。"作为回应。 以下htaccess文件适用于     http://example.com/logger/all

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !(index\.php)
RewriteRule ^(.*)$ /api/index.php/$1

但是再次添加 api 会导致"未指定输入文件。"

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !(index\.php)
RewriteRule ^api(.*)$ /api/index.php/$1

1 个答案:

答案 0 :(得分:3)

mod_rewrite是一把双刃剑,非常容易被滥用。

Apache 2.2.16及更高版本提供了非常有用但令人惊讶的很少使用FallbackResource指令。

所以使用以下布局:

'Are you sure you '\n' want to delete?'

并将以下内容用于.htaccess文件:

--api
----.htaccess
----index.php
--index.html

与基于mod_rewrite的解决方案的主要区别在于URI中没有替换,因此您必须使用FallbackResource /api/index.php 在PHP中解析URI。

例如,在此处,如果您访问$_SERVER['REQUEST_URI'],您的http://example.com/api/logger/all文件将会看到以下值:

index.php

echo $_SERVER['REQUEST_URI']; // prints /api/logger/all 文件放在.htaccess目录中的主要目的是确保只有api前缀下的网址由/api处理。任何可能导致文档根文件夹中出现404错误的URL都不会触发对FallbackResource的调用。

相关问题