Score:0

Why is adding this line to my mod_rewrite creates looping issue, and how to avoid it?

ua flag

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>
kz flag
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?
John Doisneau avatar
ua flag
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?
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.