【问题标题】:Apache Rewrite if file not found to another file?如果找不到文件到另一个文件,Apache Rewrite?
【发布时间】:2016-05-30 20:45:34
【问题描述】:

如果我的 url 不存在,我正在尝试将它们重写到另一个文件。我的网站根目录中有一个.htaccess 文件,如下所示。

RewriteEngine on

# Add slash to end of request
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]

# API redirect
RewriteCond %{REQUEST_URI} ^/api
RewriteRule . api/v1.php [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^ secondary\.
RewriteRule . sites/secondary [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . sites/main.php [L]

我的目的是为同一个文件夹中的两个站点提供服务,并使用重写来指向适当的路由器。 api 重定向效果很好。但是,除非我指定example.com 以外的路由(example.com/test/ 正确重写),否则主重定向和辅助重定向不起作用。

非常感谢任何见解。

【问题讨论】:

    标签: apache .htaccess mod-rewrite url-rewriting


    【解决方案1】:

    用途:

    RewriteEngine on
    
    # Add slash to end of request
    RewriteCond %{REQUEST_URI} !(/$|\.)
    RewriteRule ^ %{REQUEST_URI}/ [R=301,L]
    
    # API redirect
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^api api/v1.php [L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{HTTP_HOST} ^ secondary\.
    RewriteRule ^ sites/secondary [L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ sites/main.php [L]
    

    因为.至少等于一个字符。

    【讨论】:

    • 谢谢!我没想到。
    猜你喜欢
    • 2012-04-23
    • 1970-01-01
    • 2011-11-14
    • 1970-01-01
    • 1970-01-01
    • 2011-07-05
    • 1970-01-01
    • 2023-01-26
    • 1970-01-01
    相关资源
    最近更新 更多