【问题标题】:Wildcard subdomain and subfolders as parameters in .htaccess通配符子域和子文件夹作为 .htaccess 中的参数
【发布时间】:2013-09-13 16:32:06
【问题描述】:

我有一个门户网站http://www.mysite.com/,客户可以在其中注册,然后他们会获得自己的网站子域版本来运行我的应用程序。 我已经设置了通配符子域 DNS/VirtualHost 等并使其正常工作。

我要设置的是我的 htaccess 文件,以将通配符子域作为参数传递给应用程序,如下所示:

http://wildcardsubdomain.mysite.com/ -> http://www.mysite.com/app/?dj=wildcardsubdomain
http://wildcardsubdomain.mysite.com/?l=genres -> http://www.mysite.com/app/?dj=wildcardsubdomain&l=genres

我通过在http://www.mysite.com/ 的 .htaccess 文件中添加这个来实现这一点

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.mysite.com
RewriteCond %{HTTP_HOST} ^(.+).mysite.com
RewriteRule ^([^/]*)$ http://www.mysite.com/app/dj/%1 [P,L]

但我需要按如下方式重定向管理员用户 URL,并且在 /app 目录中使用 htaccess 代码时遇到了困难。 我需要的是:

http://wildcardsubdomain.mysite.com/admin/?l=genres -> http://www.mysite.com/app/admin.php?dj=wildcardsubdomain&l=genres
http://wildcardsubdomain.mysite.com/admin/?l=users -> http://www.mysite.com/app/admin.php?dj=wildcardsubdomain&l=users

(l参数不同,我这里只是以用户和流派为例)

我已经在 /app/.htaccess 中尝试过,但它并没有真正奏效:

RewriteRule ^dj/([^/]+)/?$ $2?dj=$1 [QSA,L]
RewriteRule ^admin/([^/]+)/?$ admin.php$2?dj=$1 [QSA,L]

谁能提出更好的重写规则? 非常感谢提前

【问题讨论】:

    标签: .htaccess url redirect mod-rewrite wildcard-subdomain


    【解决方案1】:

    您缺少QSA 标志,否则l=genres 会丢失。您只需要在其他规则之前有一个“管理员”规则:

    RewriteEngine on
    
    RewriteCond %{HTTP_HOST} !^www\.mysite.com
    RewriteCond %{HTTP_HOST} ^(.+).mysite.com
    RewriteRule ^admin/?$ http://www.mysite.com/app/admin.php?dj=%1 [P,L,QSA]
    
    RewriteCond %{HTTP_HOST} !^www\.mysite.com
    RewriteCond %{HTTP_HOST} ^(.+).mysite.com
    RewriteRule ^([^/]*)$ http://www.mysite.com/app/dj=%1 [P,L,QSA]
    

    【讨论】:

    • 嗨,谢谢。将主站点 htaccess 替换为您的有效代码,但不适用于管理链接(请参阅我的原始帖子。) app/ 目录 htaccess:#app original rules Options -MultiViews Options -Indexes RewriteEngine On RewriteRule ^index.html index.php RewriteRule ^admin admin.php # my rules RewriteRule ^dj/([^/]+)/?$ $2?dj=$1 [QSA,L] RewriteRule ^admin/([^/]+)/?$ admin.php$2?dj=$1 [QSA,L] 我是否需要更改 /app 文件夹 htaccess 文件中的规则?跨度>
    • @LucasYoung 立即尝试
    猜你喜欢
    • 2014-12-30
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    • 2019-07-10
    • 1970-01-01
    • 2018-07-25
    • 2011-06-05
    • 1970-01-01
    相关资源
    最近更新 更多