【问题标题】:.htaccess - redirect from subdirectory to root directory for non existing files.htaccess - 为不存在的文件从子目录重定向到根目录
【发布时间】:2012-09-26 18:43:25
【问题描述】:

当前目录结构:

wwwpublic
|+ spanish(language)
|-- index.php
|-- contact.php
|
|- index.php
|- aboutus.php
|- products.php
|- contact.php

在 wwwpublic 目录的根目录中,我有英文文件。由于网站也有西班牙语,我创建了名为“西班牙语”的单独目录。两个目录中的页面名称完全相同。

现在,西班牙语目录中的某些页面不存在,我需要将请求重定向到这些页面到根文件夹并保留请求的文件名。

示例: 访问者转到西班牙语版本,在那里打开 index.php,然后他点击西班牙语目录中的 aboutus.php 页面(不存在),然后 .htaccess 将他重定向到 /root/aboutus.php

【问题讨论】:

    标签: .htaccess redirect


    【解决方案1】:

    在您的 wwwpublic 目录中尝试这样的操作,最好在您可能拥有的任何路由规则之前:

    RewriteEngine On
    
    # conditions to check that current request doesn't point to a valid resource
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    # condition to check that request is for the /spanish/ directory
    RewriteCond %{REQUEST_URI} ^/spanish/(.+)$
    
    # conditions to check if the /spanish/ is removed whether it would point to a valid resource
    RewriteCond %{DOCUMENT_ROOT}/%1 -f [OR]
    RewriteCond %{DOCUMENT_ROOT}/%1 -d
    
    # all conditions match, rewrite
    RewriteRule ^/?spanish/(.+)$ /$1 [L]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-04
      • 2021-04-04
      • 2012-02-20
      • 2017-01-29
      • 2021-09-03
      • 2011-01-19
      相关资源
      最近更新 更多