regex - Apache mod 重写简单重定向

标签 regex apache mod-rewrite

我希望对我的网站的请求按如下方式工作:

http://example.com/理想情况下会拉出index.php文件(当前默认行为)而不显示index.php

http://example.com/foo.php将按预期拉出 foo.php

http://example.com/blerg将重定向到 http://example.com/bar.php?code=blerg

我现在有以下重写规则

    RewriteRule ^/(.*\.(php|html|htm|css|jpg))$ /$1 [NC,L]
    RewriteRule ^/(.*)$ /bar.php?code=$1 [NC,L]

几乎可以工作,除了 http://example.com/拉出 bar.php 而不是 index.php

理想情况下,我不必在第一条规则中包含所有可能的文件扩展名,我宁愿它只检测它是否是实际文件。

最佳答案

我意识到这并不完全是您所要求的,但我经常在 .htaccess 中使用它:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php

..将任何不是实际文件或目录的内容发送到index.php,然后index.php 包含解释URL 字符串中内容的逻辑。

例如

$url_array = split('/', $_SERVER['REQUEST_URI']);
array_shift($url_array); // remove first value as it's empty

关于regex - Apache mod 重写简单重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/838080/

相关文章:

php - 隐藏获取变量

javascript - Jmeter JSON Extractor 用于获取响应中的 token 值

regex - Delphi - 用户指定的字符串操作

apache - Apache 和 conf.d 安装过程的 Autoconf 宏?

php - .htaccess 拒绝所有,包括 htaccess 文件和显示 404 - 我该怎么做?

.htaccess - 用一行 htaccess 让 URL 变得漂亮

php - 如何检测文件名中的常见字符串组

r - 用A代替B,用B代替字符串中的A

python - Ubuntu 22.04 上的 Django mod_wsgi Apache 错误 403 Forbidden

node.js - Nodejs 和 Angularjs,清理 url