Chin Leung Chin Leung - 4 months ago 31
PHP Question

301 Redirection is appending ?_url

I'm trying to setup 301 redirections for a website that's build with Phalcon Framework.

The Problem



My redirection is working fine, but it's appending a
?_url=
with the old url after the redirection.

Default .htaccess



Here's what comes with the Phalcon Framework in the
.htaccess
:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]


Example



When a user is going on
http://example.net/fr/le-circuit/a-propos
, it's redirecting to
http://example.net/fr/circuit?_url=/fr/le-circuit/a-propos


As you can see, the
?_url=
is the extra that I'm trying to remove.

My .htaccess



Here's my
.htaccess
without the redirection:

AddDefaultCharset UTF-8

<IfModule mod_rewrite.c>
RewriteEngine On

# Redirection for old website links

RewriteCond %{HTTP:Accept-Language} ^fr [NC]
RewriteRule ^$ /fr/ [L,R=301]

RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteRule ^$ /en/ [L,R=301]

RewriteRule ^$ /fr/ [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
</IfModule>


Attempt #1



I've tried the following Redirect directive
before
and
after
the default rewrite rules that came with Phalcon.

Redirect 301 /fr/le-circuit/a-propos /fr/circuit


Attempt #2



I thought my first attempt wasn't strict enough so I decided to use regex and RedirectMatch:

RedirectMatch 301 ^/fr/le-circuit/a-propos/?$ /fr/circuit


Just like before, I've tried it before and after the rules that came with Phalcon and it's appending the old url after the redirection.

Attempt #3



I've tried to hardcode the url in the redirection to see if the
?_url=
would still append and ... yes it does.

RewriteMatch 301 ^/fr/le-circuit/a-propos/?$ http://example.net/fr/circuit


Does anyone know the problem or have an idea why it's appending the old url after the redirection?

Answer

Keep redirect rules before your other rules:

RewriteEngine On

# Redirection for old website links
RewriteRule ^fr/le-circuit/a-propos/?$ http://example.net/fr/circuit [L,NC,R=301]

RewriteCond %{HTTP:Accept-Language} ^fr [NC]
RewriteRule ^$ /fr/ [L,R=301]

RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteRule ^$ /en/ [L,R=301]

RewriteRule ^$ /fr/ [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]

Make sure to clear your browser cache while testing this change.