apache - 如果文件存在于子文件夹中,则 .htaccess 重定向

标签 apache .htaccess mod-rewrite redirect

我有以下挑战。假设从服务器请求以下文件:
/pages/subfolder/mypage.php
是否有使用 mod-rewrite 的选项来执行以下操作:

Look into a specific folder f. e. /USERMOD if the requested file (and structure) exists

If Yes, execute this file with all parameters

If Not, execute the requested file.


背景是重写看起来是否在/USERMOD 下有具有相同文件夹结构的替换。如果存在替代品,请使用此替代品,否则使用原始文件。
实际上(但不起作用)我有:
RewriteCond %{REQUEST_URI} !^/USERMOD/ [NC]
RewriteCond %{DOCUMENT_ROOT}/USERMOD/$1 -f [NC]
RewriteRule ^(.*)$ /USERMOD/$1 [L]
感谢您的任何提示和帮助!
编辑:完整的 .htaccess
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/USERMOD/ [NC]
RewriteCond %{DOCUMENT_ROOT}/USERMOD/$1 -f [NC]
RewriteRule ^(.*)$ /USERMOD/$1 [L]

RewriteCond %{REQUEST_FILENAME} ^(.*)\.(php|css|js|gif|jpg|jpeg|png)$ [NC]
RewriteRule ^(.+) - [L]

RewriteCond %{REQUEST_URI} (.*)?/admin/(.*)
RewriteRule ^(.+) - [L]

RewriteCond %{REQUEST_URI} (.*)?/images/(.*)
RewriteRule ^(.+) - [L]

RewriteCond %{REQUEST_URI} (.*)?/templates/(.*)
RewriteRule ^(.+) - [L]

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+) - [L]

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.+) - [L]

RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^(.+) - [L]

##boosted CONTENT
RewriteRule (.*/)?info/([A-Za-z0-9_-]+)\.html.* shop_content.php?gm_boosted_content=$2&%{QUERY_STRING} [PT,L]

##boosted PRODUCTS
RewriteRule (.*/)?([A-Za-z0-9_-]+)\.html product_info.php?gm_boosted_product=$2&%{QUERY_STRING} [PT,L]

##boosted CATEGORIES
RewriteRule (.*/)?([A-Za-z0-9_-]+)/?.* index.php?gm_boosted_category=$2&%{QUERY_STRING} [L]

最佳答案

有这样的初始规则:

RewriteEngine On

RewriteRule ^USERMOD/ - [NC,L]

RewriteCond %{DOCUMENT_ROOT}/USERMOD/$1 -f [NC]
RewriteRule ^(.*)$ /USERMOD/$1 [L]

# rest of your rules come here

关于apache - 如果文件存在于子文件夹中,则 .htaccess 重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26433340/

相关文章:

Django + Ec2 -> 显示 "runserver"控制台输出

.htaccess - 将没有尾部斜杠的 URL 重定向到带有尾部斜杠的 URL

php - Apache mod_rewrite 问题

apache - url 重写以将 www 重定向到 https 上的非 www

apache - .htaccess 在一行中指定特定文件

apache - Ubuntu + SVN : Could not open the requested SVN filesystem

apache - Apache Sqoop 真的是 Apache Hadoop 所必需的吗?有没有其他方法可以使用数据库输入在 hadoop 中进行处理?

php - 浏览器同步 - PHP 和 htaccess?

html - 使用不同的 URL 加载同一页面?

html - 使用 htaccess rewrite 使子目录成为其自己的根目录以用于根相对路径请求