Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用.htaccess应用多个规则_.htaccess_Mod Rewrite - Fatal编程技术网

使用.htaccess应用多个规则

使用.htaccess应用多个规则,.htaccess,mod-rewrite,.htaccess,Mod Rewrite,我当前的.htaccess如下所示,目前具有确保https访问的功能: 重新启动发动机 重写条件%{HTTPS}关闭 重写规则^(.*)$https://%{HTTP_HOST}%{REQUEST_URI}[L,R=301] 但是,我希望它能做得更多,因为我目前的链接结构对SEO并不友好。基本上,我希望进行更改,以便我可以在左侧键入URL,并加载右侧的URL https://nexuscompendium/heroes/li-li/ --> https://nexuscompendiu

我当前的.htaccess如下所示,目前具有确保https访问的功能:


重新启动发动机
重写条件%{HTTPS}关闭
重写规则^(.*)$https://%{HTTP_HOST}%{REQUEST_URI}[L,R=301]
但是,我希望它能做得更多,因为我目前的链接结构对SEO并不友好。基本上,我希望进行更改,以便我可以在左侧键入URL,并加载右侧的URL

https://nexuscompendium/heroes/li-li/ --> https://nexuscompendium.com/hero.php?h=li-li
https://nexuscompendium/heroes/       --> https://nexuscompendium.com/heroes/
我想出了以下似乎很好的办法。。。只要上面的HTTPS规则不是活动的,因为其他规则只是再次删除“HTTPS://”

重写规则英雄\/role\/([a-zA-Z]+)(\/?)$heroole.php?r=$1[L]
重写规则英雄\/([a-zA-Z \-]+)(\/?)$hero.php?h=$1[L]
重写规则战场\/([a-zA-Z \-]+)(\/?)$battleground.php?b=$1[L]
重写规则旋转\/([0-9\-]+)(\/?)$rotation.php?r=$1[L]
重写规则sales\/([0-9\-]+)(\/?)$sale.php?s=$1[L]
重写规则预测\/([0-9\-]+)(\/?)$predictions.php?w=$1[L]
重写规则宇宙\/([a-zA-Z \-]+)(\/?)$universe.php?u=$1[L]
重写规则子宇宙\/([a-zA-Z \-]+)(\/?)$subuniverse.php?s=$1[L]
重写规则事件\/seasonal\/([a-zA-Z\-]+)(\/?)$seasonal.php?s=$1[L]
重写规则事件\/([a-zA-Z0-9\-]+)(\/?)$event.php?e=$1[L]
重写规则皮肤\/([a-zA-Z0-9\-]+)(\/?)$skin.php?s=$1[L]
重写规则挂载\/([a-zA-Z0-9\-]+)(\/?)$mount.php?m=$1[L]
重写规则([a-zA-Z]+)(\/?)$$1.php
长话短说:我可以将上述规则结合起来吗?如果可以,如何结合?如果没有,是否有其他方法确保HTTPS重定向

提前谢谢❤

编辑:多亏了.htaccess循环直到没有更多的规则可以完成(加上在上一个重写规则中略微添加了^


重新启动发动机
重写条件%{HTTPS}关闭
重写规则^(.*)$https://%{HTTP_HOST}%{REQUEST_URI}[L,R=301]
重写规则^heroes\/role\/([a-zA-Z]+)(\/?)$herorole.php?r=$1[L]
重写规则^HERONES\/([a-zA-Z\-]+)(\/?)$hero.php?h=$1[L]
重写规则^battlegrounds\/([a-zA-Z\-]+)(\/?)$battleground.php?b=$1[L]
重写规则^rotations\/([0-9\-]+)(\/?)$rotation.php?r=$1[L]
重写规则^sales\/([0-9\-]+)(\/?)$sale.php?s=$1[L]
重写规则^predictions\/([0-9\-]+)(\/?)$predictions.php?w=$1[L]
重写规则^subuniverses\/([a-zA-Z\-]+)(\/?)$subuniverse.php?s=$1[L]
重写规则^universes\/([a-zA-Z\-]+)(\/?)$universe.php?u=$1[L]
重写规则^events\/seasonal\/([a-zA-Z\-]+)(\/?)$seasonal.php?s=$1[L]
重写规则^events\/([a-zA-Z0-9\-]+)(\/?)$event.php?e=$1[L]
重写规则^skins\/([a-zA-Z0-9\-]+)(\/?)$skin.php?s=$1[L]
重写规则^mounts\/([a-zA-Z0-9\-]+)(\/?)$mount.php?m=$1[L]
重写规则^([a-zA-Z]+)(\/?)$$1.php

多亏了arkascha的知识,.htaccess循环直到没有更多的规则可以完成为止(加上在上一个重写规则中添加了^),我使它工作起来,如下所示:


重新启动发动机
重写条件%{HTTPS}关闭
重写规则^(.*)$https://%{HTTP_HOST}%{REQUEST_URI}[L,R=301]
重写规则^heroes\/role\/([a-zA-Z]+)(\/?)$herorole.php?r=$1[L]
重写规则^HERONES\/([a-zA-Z\-]+)(\/?)$hero.php?h=$1[L]
重写规则^battlegrounds\/([a-zA-Z\-]+)(\/?)$battleground.php?b=$1[L]
重写规则^rotations\/([0-9\-]+)(\/?)$rotation.php?r=$1[L]
重写规则^sales\/([0-9\-]+)(\/?)$sale.php?s=$1[L]
重写规则^predictions\/([0-9\-]+)(\/?)$predictions.php?w=$1[L]
重写规则^subuniverses\/([a-zA-Z\-]+)(\/?)$subuniverse.php?s=$1[L]
重写规则^universes\/([a-zA-Z\-]+)(\/?)$universe.php?u=$1[L]
重写规则^events\/seasonal\/([a-zA-Z\-]+)(\/?)$seasonal.php?s=$1[L]
重写规则^events\/([a-zA-Z0-9\-]+)(\/?)$event.php?e=$1[L]
重写规则^skins\/([a-zA-Z0-9\-]+)(\/?)$skin.php?s=$1[L]
重写规则^mounts\/([a-zA-Z0-9\-]+)(\/?)$mount.php?m=$1[L]
重写规则^([a-zA-Z]+)(\/?)$$1.php

我选择在这里提供另一个答案。这并不是因为你的答案是错误的,而是因为你自己找到了一个可行的设置

但是,您的实现可以进行相当多的优化。是的,您所实现的是完全可能的,但是应用的规则越多,http服务器的速度就越慢(我们在这里讨论的是正则表达式匹配,这是一项非常昂贵的操作)。因此,可能值得对其进行一些压缩:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301]

    RewriteRule ^/?heroes/role/([a-zA-Z]+)(/?)$       /herorole.php?r=$1 [END]
    RewriteRule ^/?events/seasonal/([a-zA-Z-]+)(/?)$ /seasonal.php?s=$1 [END]

    RewriteCond %{REQUEST_URI} ^/?(\w+)s/([\w-]+)(/?)$
    RewriteCond /%1.php -f
    RewriteRule ^ /%1.php?h=%2 [END]

    RewriteRule ^/?([a-zA-Z]+)(/?)$                    /$1.php                
</IfModule>

重新启动发动机
重写条件%{HTTPS}关闭
重写规则^https://%{HTTP\u HOST}%{REQUEST\u URI}[R=301]
重写规则^/?heroes/role/([a-zA-Z]+)(/?)$/herorole.php?r=$1[结束]
重写规则^/?events/seasonal/([a-zA-Z-]+)(/?)$/seasonal.php?s=$1[结束]
重写条件%{REQUEST_URI}^/?(\w+)s/([\w-]+)(/?)$
重写cond/%1.php-f
重写规则^/%1.php?h=%2[结束]
重写规则^/?([a-zA-Z]+)(/?)$/$1.php
以下是一些优化:

  • 重定向到https中的模式不是必需的,因为您不会使用它捕获的内容。因此,请使用最简单的模式:
    ^
    ,它始终匹配,因为每个字符串都有一个开头
  • 在重写规则中的模式开头使用
    ^/?
    ,可以使其在动态配置文件(.htaccess)或真实的ht中工作