我有以下挑战。假设从服务器请求以下文件:
/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/