skip to Main Content

My current .htaccess looks like this, and currently has the function of ensuring https-access:

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

But, I want it to do more than that, as my current link-structure isn’t really SEO-friendly. Basically, I want to make changes so that I can type in the URL on the left, and have the URL on the right loaded.

https://nexuscompendium/heroes/li-li/ --> https://nexuscompendium.com/hero.php?h=li-li
https://nexuscompendium/heroes/       --> https://nexuscompendium.com/heroes/

I’ve come up with the following that seems to work just fine… as long as the HTTPS-rule above isn’t also active, as the other rules simply removes the “https://” again.

    RewriteRule heroes/role/([a-zA-Z]+)(/?)$       herorole.php?r=$1     [L]
    RewriteRule heroes/([a-zA-Z-]+)(/?)$           hero.php?h=$1         [L]
    RewriteRule battlegrounds/([a-zA-Z-]+)(/?)$    battleground.php?b=$1 [L]
    RewriteRule rotations/([0-9-]+)(/?)$           rotation.php?r=$1     [L]
    RewriteRule sales/([0-9-]+)(/?)$               sale.php?s=$1         [L]
    RewriteRule predictions/([0-9-]+)(/?)$         predictions.php?w=$1  [L]
    RewriteRule universes/([a-zA-Z-]+)(/?)$        universe.php?u=$1     [L]
    RewriteRule subuniverses/([a-zA-Z-]+)(/?)$     subuniverse.php?s=$1  [L]
    RewriteRule events/seasonal/([a-zA-Z-]+)(/?)$ seasonal.php?s=$1     [L]
    RewriteRule events/([a-zA-Z0-9-]+)(/?)$        event.php?e=$1        [L]
    RewriteRule skins/([a-zA-Z0-9-]+)(/?)$         skin.php?s=$1         [L]
    RewriteRule mounts/([a-zA-Z0-9-]+)(/?)$        mount.php?m=$1        [L]
    RewriteRule ([a-zA-Z]+)(/?)$                     $1.php

Long story short: Can I combine the rules above – and, if so, how? If not, is there a way to ensure the HTTPS-redirect otherwise?

Thanks in advance ❤

EDIT: Thanks to the knowledge that .htaccess loops until no more rules can be completed (along with the slight addition of ^ in the last RewriteRule, I got it working, as follows:

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

<IfModule mod_rewrite.c>
    RewriteRule ^heroes/role/([a-zA-Z]+)(/?)$       herorole.php?r=$1     [L]
    RewriteRule ^heroes/([a-zA-Z-]+)(/?)$           hero.php?h=$1         [L]
    RewriteRule ^battlegrounds/([a-zA-Z-]+)(/?)$    battleground.php?b=$1 [L]
    RewriteRule ^rotations/([0-9-]+)(/?)$           rotation.php?r=$1     [L]
    RewriteRule ^sales/([0-9-]+)(/?)$               sale.php?s=$1         [L]
    RewriteRule ^predictions/([0-9-]+)(/?)$         predictions.php?w=$1  [L]
    RewriteRule ^subuniverses/([a-zA-Z-]+)(/?)$     subuniverse.php?s=$1  [L]
    RewriteRule ^universes/([a-zA-Z-]+)(/?)$        universe.php?u=$1     [L]
    RewriteRule ^events/seasonal/([a-zA-Z-]+)(/?)$ seasonal.php?s=$1     [L]
    RewriteRule ^events/([a-zA-Z0-9-]+)(/?)$        event.php?e=$1        [L]
    RewriteRule ^skins/([a-zA-Z0-9-]+)(/?)$         skin.php?s=$1         [L]
    RewriteRule ^mounts/([a-zA-Z0-9-]+)(/?)$        mount.php?m=$1        [L]
    RewriteRule ^([a-zA-Z]+)(/?)$                    $1.php                
</IfModule>

2

Answers


  1. Chosen as BEST ANSWER

    Thanks to the knowledge from arkascha that .htaccess loops until no more rules can be completed (along with the slight addition of ^ in the last RewriteRule), I got it working, as follows:

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{HTTPS} off
        RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    </IfModule>
    
    <IfModule mod_rewrite.c>
        RewriteRule ^heroes/role/([a-zA-Z]+)(/?)$       herorole.php?r=$1     [L]
        RewriteRule ^heroes/([a-zA-Z-]+)(/?)$           hero.php?h=$1         [L]
        RewriteRule ^battlegrounds/([a-zA-Z-]+)(/?)$    battleground.php?b=$1 [L]
        RewriteRule ^rotations/([0-9-]+)(/?)$           rotation.php?r=$1     [L]
        RewriteRule ^sales/([0-9-]+)(/?)$               sale.php?s=$1         [L]
        RewriteRule ^predictions/([0-9-]+)(/?)$         predictions.php?w=$1  [L]
        RewriteRule ^subuniverses/([a-zA-Z-]+)(/?)$     subuniverse.php?s=$1  [L]
        RewriteRule ^universes/([a-zA-Z-]+)(/?)$        universe.php?u=$1     [L]
        RewriteRule ^events/seasonal/([a-zA-Z-]+)(/?)$ seasonal.php?s=$1     [L]
        RewriteRule ^events/([a-zA-Z0-9-]+)(/?)$        event.php?e=$1        [L]
        RewriteRule ^skins/([a-zA-Z0-9-]+)(/?)$         skin.php?s=$1         [L]
        RewriteRule ^mounts/([a-zA-Z0-9-]+)(/?)$        mount.php?m=$1        [L]
        RewriteRule ^([a-zA-Z]+)(/?)$                    $1.php                
    </IfModule>
    

  2. I chose to offer another answer here. Not because yours is wrong, it is great that you figured out a working setup yourself!

    However your implementation can be optimized quite a bit. Yes, what you implemented is perfectly possible, but the more rules get applied the more the http server get’s slowed down (we are talking about regular expression matching here, that is quite an expensive operation). So it might be worth to condense this a bit:

    <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>
    

    Here some of the optimizations:

    1. the pattern in the redirection to https is not required, since you do not use what it captures anyway. So use the most simple pattern there is: ^ which always matches, since every string has a beginning.
    2. Using ^/? at the start of a pattern in a rewriting rule makes it work in dynamic configuration files (“.htaccess”) or i nthe real http server configuration. More on that further down.
    3. most of the escape characters () are not required. Leaving them away makes the pattern much easier to read.
    4. redirect to absolute path if possible, that can help to prevent confusion.
    5. use [END] instead of [L] in most cases if your http server is not too old … It terminates the whole rewriting process if that rule gets applied, while the older [L] flag only terminates this run of the rewriting process, so the process loops again. That wastes quite some time, since certainly in this situation no other rule will get applied. So you save one run for every request.
    6. you can combine some of the rules to a more general one which again reduces the number of rules. This is especially true the more rules come together over time…
    7. do you really need case insensivity ([a-zA-Z])? You know the references you hand out, you control them. And no one types those by hand, right? Also you often can use w instead of [a-z], easier, though not exactly the same. As said: often, but it makes things easier to read again.
    8. also decide if you really need the <IfModule mod_rewrite.c>. It prevents an internal error when the rewriting module is not installed, sure. But what for? Usually the application won’t work without anyway…

    OK, there is certainly more that can be said here. But this might be enough now give you a starting point. So I will end with a few general remarks:


    It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out…

    In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.

    This implementation will work likewise in the http servers host configuration or inside a dynamic configuration file (“.htaccess” file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it’s interpretation is enabled at all in the host configuration and that it is located in the host’s DOCUMENT_ROOT folder.

    And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (“.htaccess”). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search