.htaccess - 如何使用 htaccess 和 mod_rewrite 在内部重定向所有内容?

标签 .htaccess url mod-rewrite redirect url-rewriting

我想使用单个 php 脚本处理对域的所有请求。

例如:

www.domain.com/something.php -> would be loading -> www.domain.com/process.php?u=something.php
www.domain.com/anything      -> would be loading -> www.domain.com/process.php?u=anything
www.domain.com/sub/dir/x     -> would be loading -> www.domain.com/process.php?u=sub/dir/x

但是 process.php 永远不应该直接在浏览器上打开,我想做一个内部重定向。

有人可以告诉我如何使用 htaccess 和 mod_rewrite 来做到这一点吗?

最佳答案

将这些行放入您的 .htaccess 文件中:

RewriteEngine On
RewriteRule (.*) process.php?u=$1 [QSA,L]

这将告诉 Apache 使用 process.php 而不是实际请求的页面。

很可能您还需要在 RewriteRule 行之前添加这些行,以告诉 Apache 按原样处理现有文件,否则您的图像/css/javascript 文件也将由 process 处理.php

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

关于.htaccess - 如何使用 htaccess 和 mod_rewrite 在内部重定向所有内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6397852/

相关文章:

apache - 仅使用我在 htaccess 上编写的 url 进行访问,防止访问真实的 url

android - 如何修复 URL 中的空格错误

magento - 仅从前端而不是在 magento 的后端删除 index.php

regex - htaccess强制使用https并将www重定向到非www,但没有其他子域

python - 使用像 jsbin 这样的哈希处理 url

regex - 如何匹配 RewriteCond 中的子目录?

css - 错误文档无样式(服务器)

php - htaccess 制作 "pretty"URL

php - 使用 htaccess 的多个 rewriterole url

java - URL中的方括号导致Tomcat异常