【问题标题】:htaccess routing with url parameters带有url参数的htaccess路由
【发布时间】:2019-01-08 10:10:33
【问题描述】:

enter code here您好,我的 htaccess 文件中有以下代码。

我的通配符子域路由到“主文件夹” - 我在这里放置了 htaccess 文件。

我有以下文件夹 “主文件夹” “主文件夹/子 1” “主文件夹/子 2” 等等

调用子域 - sub1.domain.com 它应该路由到子文件夹“sub1”(子文件夹=子域)。

我试着用这段代码来做

#Redirect to subdomainfolder if no special page is called
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteRule ^$ %1/index.html [L]

#Redirec to subdomainfolder if a special page is called
RewriteCond %{HTTP_HOST} ^(.*)\.domain.com(.*)$  [NC]
RewriteRule ^(.*) %1/$1 [L]

第一个规则运行良好,但如果我添加第二个规则,我会收到内部服务器错误。

这有什么问题 - 如何以这种方式更改第一条规则,使其适用于 .com 之后的所有 url 参数 - 这就是我添加第二条规则的原因。

希望我能得到帮助。非常感谢。

【问题讨论】:

    标签: php .htaccess


    【解决方案1】:

    如今,许多网络主机都在其管理面板中提供了一种轻松实现子域创建的方法。你只需要去那里,选择你的子域名,然后将它指向你树中的一个目录。

    如果你不能,那么它会稍微复杂一些(你需要将该子域解析为你的服务器 ip,配置一些虚拟主机......等等)并且你可能没有足够的权限来做到这一点(除非您在专用服务器上)。

    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^example.com [NC]
    RewriteRule ^(.*)$ http://www.example.com$1 [L,R=301]
    RedirectMatch 301 ^/subfolder/(.*)$ http://subdomain.example.com/$1
    

    喜欢

    RewriteEngine on
    
    RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
    RewriteRule ^sub1/(.*)$ http://sub1.example.com/$1 [L,QSA,R=301]
    
    RewriteCond %{HTTP_HOST} ^sub1\.example\.com$
    RewriteCond %{REQUEST_URI} !^sub1/
    RewriteRule ^(.*)$ /sub1/$1 [L,QSA]
    

    如果有更多了解,请关注https://beginnersbook.com/2013/08/redirecting-from-subdirectory-to-subdomain-using-htaccess/

    【讨论】:

    猜你喜欢
    • 2018-08-31
    • 2015-10-06
    • 2017-08-10
    • 1970-01-01
    • 2015-10-30
    • 2018-04-05
    • 2015-09-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多