【问题标题】:How to redirect users to the desktop site if the files doesn't exist in the mobile site?如果移动站点中不存在文件,如何将用户重定向到桌面站点?
【发布时间】:2014-10-20 02:44:34
【问题描述】:

我创建了www.example.comm.example.comm.example.com 位于 htdocs/m/ 文件夹内。我在.htaccess 中使用以下代码:

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]

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

# Resolve .php file for extension-less php urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]

现在,如果用户点击m.example.com/abc 并且如果abc 文件不存在,它会将用户重定向到错误页面,因为我在.htaccess 代码中有以下代码在htdocs/m/ 文件夹内

ErrorDocument 404 http://m.example.com/error

但现在我希望将用户重定向到www.example.com/abc,因为abc 可能是桌面版本中的有效文件。仅当桌面站点中也不存在文件时,用户才会被重定向到错误页面。

【问题讨论】:

    标签: php .htaccess redirect mobile subdomain


    【解决方案1】:

    您可以将此规则作为第一条规则放在DocumentRoot of m.example.com

    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME}\.php !-f
    RewriteRule ^(.+)$ http://www.example.com/$1 [L,R=302]
    

    【讨论】:

    • 谢谢。但是现在所有链接都被重定向到 www.example.com/page,即使文件存在......
    • 由于RewriteCond %{REQUEST_FILENAME} !-f 条件,无法重定向现有文件。您确定 /page现有文件吗?
    • /page.php 是现有文件。
    • 嗨@anubhava,你能在这个方面提供更多帮助吗?- stackoverflow.com/questions/26531318 无法弄清楚如何正确配置...谢谢!
    • 我一定会看看的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-28
    • 2014-07-12
    • 1970-01-01
    相关资源
    最近更新 更多