【发布时间】:2019-01-19 11:39:54
【问题描述】:
我是在 apache 上使用 mod_rewrite 的新手,我目前正在尝试编写一个 url-shortener,它采用像 mydomain/s/short/{UriToShorten} 这样的 URL 并缩短给定的 URI。
会发生什么强>
当我在浏览器中将 URI 传递给缩短路径时,例如 localhost/s/short/http://example.com ,将调用脚本 highlight.php 并且缩短过程按预期工作。但是,如果我对 URL-Component 进行编码并传递像 localhost/s/short/http%3A%2F%2Fexample.com 这样的 URI(这应该是正确的用法),我会收到消息
未找到
在此服务器上找不到请求的 URL /s/short/http://example.com。
调试表明,shortcut.php 或 index.php 都没有运行(所以我相信,服务器确实尝试搜索 short 的子目录 http:)。这似乎在斜杠编码 %2F 所在的位置独立发生。
预期结果
我希望编码版本可以工作,并且可能未编码的 URI 会导致错误(因为 URL 编码是为了避免这些错误而专门进行的)。
我使用什么
我将以下内容用作 .htaccess:
RewriteEngine On
RewriteBase /s/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^short/(.*)$ shorten.php?url=$1 [L,NC]
RewriteRule ^([a-z0-9]{5})$ index.php?slug=$1 [L,NC]
我的文件夹结构是:
-www
-s
-.htaccess
-shorten.php
-index.php
我的 Apache 版本是 Apache/2.4.33
我尝试了什么
由于我不知道为什么会发生这种情况,我只能搜索 mod_rewrite 文档并尝试添加 [NE] Flag,这显然不起作用。
【问题讨论】:
标签: apache .htaccess mod-rewrite