0

I pasted below the existing mod_rewrite section of the .htaccess of my Magento site.

I need to modify it to implement a short URL scheme that would redirect all requests like mysite.com/-jk7ASD63 (all short URLs starting with a dash) to mysite.com/cartroute/cartlink?shortcode=jk7ASD63 (long URLs with dash removed).

I believe this is the right line to add (I tested it here https://htaccess.madewithlove.com/):

RewriteRule ^-([a-zA-Z0-9]+)$ cartroute/cartlink?shortcode=$1 [L]

I tried to add this line in various places inside the mod_rewrite section, but everytime it created looping issues that I don't understand... Sure, once the full route is created, it goes through the mod_rewrite again, but why doesn't it redirect to the correct link?

Current mod_rewrite section of the .htaccess:

<IfModule mod_rewrite.c>

    Options +FollowSymLinks
    RewriteEngine on

    RewriteRule ^api/rest api.php?type=rest [QSA,L]

    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    RewriteCond %{REQUEST_METHOD} ^TRAC[EK]
    RewriteRule .* - [L,R=405]

    <IfModule mod_setenvif.c>
        <IfModule mod_headers.c>
            Header set X-Content-Type-Options: nosniff
            BrowserMatch \bMSIE\s8 ie8
            Header set X-XSS-Protection: "1; mode=block" env=!ie8
        </IfModule>
    </IfModule>

    RewriteCond %{REQUEST_URI} !^/(media|skin|js)/

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteCond %{REQUEST_URI} !^/index.php/

    RewriteRule .* index.php [L]

</IfModule>
  • How is the URL `/cartroute/cartlink?shortcode=jk7ASD63` meant to be handled? This does not look like a valid endpoint (that actually handles the request). This would be internally rewritten a second time to `index.php`, but the intermediary rewrite would be lost (unless you have some specific code that looks for this). Perhaps this should be an external redirect (as would usually be used with a URL-shortener type service), not an internal rewrite? – MrWhite Jun 06 '23 at 23:16
  • I have created a controller to handle these URLs `/cartroute/cartlink?shortcode=jk7ASD63` and this controller is tested and working. Yes, you are right, debugging the .htaccess I actually saw that the second redirection was made to index.php. I just don't get that if I go to `/cartroute/cartlink?shortcode=jk7ASD63` directly, everything is fine. Could you detail how you would edit this to make an external redirect? – John Doisneau Jun 07 '23 at 17:59

0 Answers0