【问题标题】:.htaccess silent redirection to subfolder.htaccess 静默重定向到子文件夹
【发布时间】:2014-07-23 15:28:37
【问题描述】:

如果文件夹/文件不存在,我想将域的所有请求静默重定向到子文件夹。

很遗憾,有些场景无法正常工作或以非静默方式工作。

  1. works: http://mydomain.com/index.html -- 显示 production/index.html 的内容

  2. 不起作用:http://mydomain.com/ -- 它不显示 production/index.html

  3. works: http://mydomain.com/test/ -- 显示 production/test/index.html 的内容

  4. 不起作用:http://mydomain.com/test -- 它将 url(而不是静默重定向)重定向到 http://mydomain.com/production/test/ 并显示 production/test/index.html 的内容

下面是 .htaccess 文件:

Options -Indexes +SymLinksIfOwnerMatch
DirectoryIndex index.php index.html
RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_URI} !^/production/

# Don't apply to URLs that go to existing files or folders
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Rewrite all those to insert /production folder
RewriteRule ^(.*)$ /production/$1 [L]

【问题讨论】:

    标签: regex apache .htaccess mod-rewrite


    【解决方案1】:

    这是因为mod_dirmod_rewrite 执行后附加了一个斜杠。改变你的规则来解决这个问题:

    DirectorySlash Off 
    Options -Indexes +SymLinksIfOwnerMatch 
    DirectoryIndex index.php index.html 
    RewriteEngine on 
    RewriteBase / 
    
    # add a trailing slash for directories 
    RewriteCond %{REQUEST_FILENAME} -d 
    RewriteCond %{THE_REQUEST} \s/+([^.]*?[^/])[?\s] 
    RewriteRule [^/]$ /%1/ [L,R=302,NE] 
    
    # take care of landing page: 
    RewriteRule ^/?$ production/ [L] 
    
    # Don't apply to URLs that go to existing files or folders 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    # Rewrite all those to insert /production folder 
    RewriteRule ^((?!production).+)$ production/$1 [L,NC]
    

    【讨论】:

    • 谢谢,不幸的是这没有帮助。上面的第 2 点和第 4 点仍然不起作用。
    • 对不起,同样的事情,除了现在 #1 也不起作用。
    • 不幸的是,我们又回到了开始的地方,第 2 点和第 4 点仍然不起作用。 (#1 再次起作用)
    • 这是经过全面测试的解决方案。在不同的浏览器中测试它以避免 301 缓存。
    • 如果 http://mydomain.com/index.html 存在,http://mydomain.com/ 也将显示 http://mydomain.com/production/index.html。那是因为您的条件是只应将不存在的文件/目录发送到/production。如果您想要例外,请告诉我。
    猜你喜欢
    • 2014-06-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-11
    • 2017-12-05
    • 2015-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多