Apache .htaccess重写现有文件和文件夹的规则

Apache .htaccess重写现有文件和文件夹的规则,apache,.htaccess,mod-rewrite,url-rewriting,Apache,.htaccess,Mod Rewrite,Url Rewriting,我的web结构如下所示: public_html/ /images/ /user/ /userimage1.jpg /userimage2.jpg /userimage3.jpg /icons/ /index.php /user.php ... <IfModule mod_rewrite.c> Options +FollowSymLinks

我的web结构如下所示:

public_html/
    /images/
        /user/
             /userimage1.jpg
             /userimage2.jpg
             /userimage3.jpg
        /icons/
    /index.php
    /user.php
...
<IfModule mod_rewrite.c>
    Options +FollowSymLinks +MultiViews
    RewriteEngine on

    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

    RewriteCond %{HTTP_HOST} ^images\.example\.com$ [NC]
    RewriteCond %{REQUEST_URI} !^/images/
    RewriteRule ^(.*)$ /images/$1 [NC,L]

    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f
    RewriteRule ^(.*)$ $1.php [L]
</IfModule>
我有两个域:
example.com
images.example.com
,我想使用
.htaccess重写规则
images.example.com
子域指向
/images/
-文件夹,但也使用不带文件扩展名的URL

我的
.htaccess
如下所示:

public_html/
    /images/
        /user/
             /userimage1.jpg
             /userimage2.jpg
             /userimage3.jpg
        /icons/
    /index.php
    /user.php
...
<IfModule mod_rewrite.c>
    Options +FollowSymLinks +MultiViews
    RewriteEngine on

    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

    RewriteCond %{HTTP_HOST} ^images\.example\.com$ [NC]
    RewriteCond %{REQUEST_URI} !^/images/
    RewriteRule ^(.*)$ /images/$1 [NC,L]

    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f
    RewriteRule ^(.*)$ $1.php [L]
</IfModule>

选项+后续符号链接+多视图
重新启动发动机
重写条件%{HTTPS}关闭
重写规则(.*)https://%{HTTP_HOST}%{REQUEST_URI}[R=301,L]
RewriteCond%{HTTP_HOST}^images\.example\.com$[NC]
重写cond%{REQUEST_URI}^/图像/
重写规则^(.*)$/images/$1[NC,L]
重写cond%{DOCUMENT\u ROOT}%{REQUEST\u URI}-D
RewriteCond%{DOCUMENT\u ROOT}%{REQUEST\u URI}\.php-f
重写规则^(.*)$$1.php[L]
现在,
https://example.com/user/
工作正常,但当我尝试打开
https://images.example.com/user/userimage1.jpg
它说
%{REQUEST_URI}
/images/redirect:/images/user.php/userimage1.jpg

不幸的是,域和子域都必须安装
public\u html
作为根文件夹


我必须如何处理我的
.htaccess
文件,以便两个URL都
https://example.com/user/
https://images.example.com/user/userimage1.jpg
工作正常?

您与
多视图
(您已在顶部启用)存在冲突。事实上,“
https://example.com/user/
工作正常”(带尾随斜杠)是因为多视图,而不是您的mod_rewrite指令。(所编写的mod_rewrite指令仅对
/user
-无尾随斜杠“起作用”。)

当您请求
https://images.example.com/user/userimage1.jpg
,多视图触发对
/user.php/userimage1.jpg
/user.php
,带有附加路径信息
/userimage1.jpg
),但mod_rewrite也尝试重写请求(内部“重定向”)-因此,重写的形式似乎不正确

通常,您需要避免使用带有mod_重写的多视图,这是冲突的常见原因

请尝试以下操作:

Options +FollowSymLinks -MultiViews
RewriteEngine on

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# Rewrite images subdomain
RewriteCond %{HTTP_HOST} ^images\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/images/
RewriteRule ^(.*)$ /images/$1 [L]

# Append .php file extensions
RewriteCond %{DOCUMENT_ROOT}/$1 !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.*)/$ $1.php [L]
请注意,我在
RewriteRule
模式中包含了尾随斜杠,并将其从捕获子模式中删除-这是假设尾随斜杠在URL上是必需的(如示例中所示)


除非mod_rewrite确实是可选的,否则您不需要
包装器?(不是。)

谢谢你的全面解释:)现在它工作正常了!