regex - 带有嵌套 .htaccess 文件的 mod_rewrite

标签 regex apache .htaccess mod-rewrite

我已经在我的根目录中创建了一个 .htaccess 以创建子域级别,假设它是重定向到 domain.ex 的 sub.domain.ex/deb/

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} sub.domain.ex
RewriteCond %{REQUEST_URI} !deb/
RewriteRule ^(.*)$ /deb/$1 [L]

这很有效。

现在,我转到 /deb/ 并使用以下内容创建另一个 .htaccess

RewriteEngine on
RewriteRule ^([^/]+)/ /deb.php?app=$1 [NC]

deb.php 是一个打印参数“app”的文件。 它有效,但只有当我调用 http://sub.domain.ex/something/ (注意末尾的斜线)时才有效。它只适用于最后一个斜线,如果我删除它,它不会,我希望它在没有最后一个斜线的情况下工作。

所以我将规则更改为 ^([^/]+) 但现在我有 500 Apache 内部错误。 正则表达式教练在我身边进行选择,也许我遗漏了一些东西。 谢谢


更新

我快疯了。 Mybe 将一个 .htaccess 放在根目录中用于创建 se 3th 子级域和另一个目录中的 .htacces 是错误的吗?因为我在尝试一些技巧。 我用这个规则

RewriteEngine on
RewriteRule ^([^/]+)/ deb.php?app=$1 [NC]

/deb 目录的.htacces 中创建了一个print_r($_GET); 并调用了index.php。因此,如果它在 cydia sublvel 的 index.php 上转发我并且不使用/deb/deb.php,那么重定向根本不起作用! 回顾一下。 我的目录结构是这样的:

/htdocs/ -> the root of the main domain level like www.example.com
---index.php -> home file of www.example.com

/htdocs/deb -> the root directory of the 3th sublevel domain (subdomain.example.com ->
---index.php
---deb.php

因此,第 3 级域的 .htaccess 位于/htdocs./htaccess 中,如前所述。 另一个用于“美化”链接的 .htaccess 在/htdocs/deb/.htaccess 中。我希望当您转到 subdomain.domain.com/someText 时它会转换为 deb.php?app=someText

现在我尝试访问 subdomain.domain.com/deb.php...WTF!? deb.php 在/htdocs/deb/deb.php

首页清晰

最佳答案

这对我来说很好,至少,假设我理解你想要的一切。

/htdocs/.htaccess 中:

RewriteEngine On
RewriteCond %{HTTP_HOST} =sub.domain.ex
RewriteCond %{REQUEST_URI} !^deb/
RewriteRule ^(.*)$ /deb/$1

/htdocs/deb/.htaccess 中:

RewriteEngine On
RewriteBase /deb/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ deb.php?app=$1
RewriteRule !^deb.php index.php

编辑:我更新了第二个文件的内容以反射(reflect)您的额外请求。如果您不希望他们能够访问 sub.domain.ex/something/-> deb.php?app=something,则应删除 /? .

关于regex - 带有嵌套 .htaccess 文件的 mod_rewrite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3105055/

相关文章:

javascript - 否定随机正则表达式

regex - 我可以在正则表达式中使用求和吗?

javascript - 使用正则表达式从字符串中删除不需要的字符

apache - Grails + Tomcat + Apache 切换到 HTTPS 时出现错误

apache - 416 请求的范围不可满足

php - url 中的 htaccess rewriterule 2 参数

java - cucumber arity 不匹配 : trying to bind several matching groups to a single stepdef parameter

apache - 关闭由 QNetworkAccessManager 处理的连接

php - 在 .htaccess 上用一些参数将 php 重写为 png

php - CakePHP 3 调试 session 超时