Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove hardcoded urls from htaccess rewriterules

I've got a .htaccess with multiple rules and 3 with a hardcoded urls for a Magento shop:

1) To remove the port number from the URL, the site is running in a Docker container with Apache and Nginx runs on the host to forward traffic to the right container. For some reason :80 is added to the url, this rule removes it:

RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://domain.nl/$1 [L,R=301]

2) Removes the index.php from the url:

RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ https://domain.nl/$1 [R=301,L]

3) Redirects /home to /:

redirect 301 /home https://domain.nl

Now I'd like to run multiple stores in this Magento installation so multiple domain names are linked to this installation. This .htaccess conflicts because of the hardcoded urls. I've tried to make these generic but I end up with 500 errors. How to make these 3 rules generic?

like image 812
Roy Avatar asked Feb 01 '26 12:02

Roy


1 Answers

Found it! After multiple attempts and checking the headers with curl I figured it out. But it's still a little strange configuration because Nginx redirects everything on the host to Apache in the Docker container.

1)

RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^([^:]+)(:[0-9]+)?$
RewriteRule ^(.*)$ https://%1/$1 [L,R=301]

2)

RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ https://%{HTTP_HOST}/$1 [R=301,L]

3)

RewriteRule ^home/?$ https://%{HTTP_HOST}/ [nc,R=301,L]
like image 178
Roy Avatar answered Feb 04 '26 00:02

Roy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!