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
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:
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:
Here some of the optimizations:
^
which always matches, since every string has a beginning.^/?
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.) are not required. Leaving them away makes the pattern much easier to read.
[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.[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 usew
instead of[a-z]
, easier, though not exactly the same. As said: often, but it makes things easier to read again.<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).