通过 RewriteCond 和 .htaccess 对 RewriteRule 进行分组 [英] Grouping RewriteRule by RewriteCond with .htaccess

查看:40
本文介绍了通过 RewriteCond 和 .htaccess 对 RewriteRule 进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网页模板,用于运行多个登录页面,htaccess 用于将友好 URL 定向到 PHP 代码中的 pageid.一切正常,但目前我必须在每个规则或重定向冲突之前添加重写条件行.您可以在下面的伪代码中看到这一点:

I have a webpage template that I use to run multiple landing pages and htaccess is used to direct the friendly URL to the pageid within the PHP code. It all works but at the moment I have to add the rewrite condition line before every rule or the redirects clash. You can see this in the pseudo code below:

RewriteEngine On

RewriteCond for domain1
RewriteRule for page 1 on domain 1
RewriteCond for domain1
RewriteRule for page 2 on domain 1

RewriteCond for domain2
RewriteRule for page 1 on domain 2
RewriteCond for domain2
RewriteRule for page 2 on domain 2

这不能很好地扩展,如果我在任何其他语言中这样做,我会将规则分组如下:

This doesn't scale well and if I was doing this any other languages I would be grouping the rules like this:

RewriteCond for domain1
{
    RewriteRule for page 1 on domain 1  
    RewriteRule for page 2 on domain 1
}

RewriteCond for domain2
{
    RewriteRule for page 1 on domain 2  
    RewriteRule for page 2 on domain 2
}

如果我可以分组,它将使 htaccess 文件更易于管理.有没有办法对规则进行分组?我曾尝试寻找解决方案,但我遇到的每个示例都讨论了 1 个域的 1 个重定向.我的另一个想法是在 PHP/MySQL 中制作一个 htaccess 脚本,我输入我的数据,按下一个按钮,它会写入 htaccess 文件,阅读起来很复杂,但没问题.我确定有一个简单的解决方案.请问有人可以帮忙吗?

If I could group, it would make the htaccess file much more manageable. Is there a way to group rules? I have tried searching for a solution but every example I come across discusses 1 redirect for 1 domain. My other idea is to make an htaccess script in PHP / MySQL which I enter my data, press a button and it writes the htaccess file, it would be complex to read but would ok. I'm sure there is a simple solution. Can anyone help please?

推荐答案

这是我在生产中工作的最终解决方案:

Here is my final solution that is working in produciton:

RewriteEngine On

<If "%{HTTP_HOST} == 'domain1.com'">        

        #main pages
        RewriteRule /members/$    /index.php?pageid=2 [NC,L]
        RewriteRule /contact/$    /index.php?pageid=3 [NC,L]
        RewriteRule /affiliates/$ /index.php?pageid=4 [NC,L]

        #blog articles
        RewriteRule /welcome-to-our-blog/$ /index.php?pageid=b0001 [NC,L]
</If>


<If "%{HTTP_HOST} == 'domain2.com'">        

        #main pages
        RewriteRule /about/$        /index.php?pageid=2 [NC,L]
        RewriteRule /press-kit/$    /index.php?pageid=3 [NC,L]
        RewriteRule /contact/$   /index.php?pageid=4 [NC,L]
        RewriteRule /affiliates/$   /index.php?pageid=5 [NC,L]

        #blog articles
        RewriteRule /welcome-to-our-blog/$ /index.php?pageid=b0001 [NC,L]
</If>

这篇关于通过 RewriteCond 和 .htaccess 对 RewriteRule 进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆