【问题标题】:Using .htaccess to rewrite dynamic subdomains使用 .htaccess 重写动态子域
【发布时间】:2013-11-19 23:41:22
【问题描述】:

我目前正在使用 .htaccess 在我的网站上重写以创建动态子域。我还使用它来删除所有页面上的 .php 扩展名。但是,一旦进入子域,它就会再次尝试重定向。例如,如果您访问https://admin.example.com/test,它实际上将访问https://example.com/clients/admin/test.php。我使用以下 .htaccess 文件不断收到各种 404 错误:

Options +MultiViews
Options +FollowSymLinks

RewriteEngine On
RewriteRule ^subdomains/(.*)/(.*) http://$1.example.com/$2 [r=301,nc]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/%2%{REQUEST_URI}/ -d
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC]

RewriteRule ^(.*)$ clients/%2/$1 [QSA,L]

我怎样才能避免重定向到https://admin.example.com/clients/admin/test.php

【问题讨论】:

    标签: php apache .htaccess mod-rewrite redirect


    【解决方案1】:

    首先,您可能想关闭多视图,这会弄乱整个“删除 php 扩展”的事情

    然后,你想要这条规则:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^\.]+)$ $1.php [NC,L]
    

    在您的重定向之后,并且您希望对实际重定向进行重定向状态检查:

    Options -Multiviews + FollowSymLinks
    
    RewriteEngine On
    
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^subdomains/(.*)/(.*) http://$1.example.com/$2 [r=301,nc]
    
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC]
    RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC]
    RewriteCond %{DOCUMENT_ROOT}/%2%{REQUEST_URI}/ -d
    RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
    
    RewriteCond %{REQUEST_URI} !^/clients/
    RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC]
    RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC]
    RewriteRule ^(.*)$ clients/%2/$1 [QSA,L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^([^\.]+)$ $1.php [NC,L]
    

    【讨论】:

    • 使用子域规则时出现 500 错误。
    • @brokekidweb 抱歉错过了一个条件
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-17
    • 2011-05-15
    • 2011-02-04
    • 2014-03-17
    • 1970-01-01
    • 1970-01-01
    • 2016-08-27
    相关资源
    最近更新 更多