How can I do a 301/permanent-redirect from a Modx document that has changed its location within the Modx structure, i.e. its friendly URL?
Friendly URLs per se are working fine and so far there was no problem with redirect permanent in .htaccess (for pages formerly having been static html pages). But now I’ve moved a document within the Modx site structure to an other location:
domain.com/folder-a/document.html to domain.com/folder-b/document.html
When doing a redirect permanent in the htaccess, modx returns the following URL to the browser:
domain.com/folder-b/document.html?q=folder-a/document.html
which of course results in a 404-error. It obviously executes the redirect directive in the htaccess correctly but then the Modx directives are adding this ?q=...
Everything works fine when I change the redirect to index.php?id=documentid style. But of course then I don’t get friendly URLs, i.e. the browser gets the index.php?id=documentid, eventually causing a duplicate document problem with Google.
Any idea how I can do a 301/permanent-redirect from a Modx document that has changed its friendly URL?
Thanks!
Franz
That’s exactly what I’m doing ... redirects first, then mod rewrite. It still behaves like described above.
added: Okay, I found a solution, in case anyone else runs into the same problem. The solution might not be very elegant, but it works:
The original file must exist in the original location. So I made many little dummy files and dummy directories. Translated to my example above:
While the actual location (in friendly URLs’ terms) is: domain.com/folder-b/document.html
the following directory and file physically has to be on the webserver for the redirect to work: domain.com/folder-a/document.html
If the original folder/file doesn’t exist, the Modx rewrite rules in the .htaccess are producing a URL in the form of: domain.com/folder-b/document.html?q=folder-a/document.html
hey fneumeier,
thanks alot for that - I was having exactly the same problem - just dropped the offending previous files into where they were and the 301 redirect is now working a treat. I’m sure this will now keep the google bot happy!
S
Just yesterday I’ve actually found a solution that works for me: In the .htaccess instead of working with
redirect 301 [orig_path] [redirect_target]
I’m now using
RewriteEngine On
RewriteBase /
RewriteRule [orig_path] [redirect_target] [L,NC,R=301]
Make sure this comes before the Modx-specific entries
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
AddType text/x-component .htc
Only disadvantage: You run into trouble when a filename you’re redirecting this way is being used as an actual SEO-friendly URL within Modx, i.e. you’re redirecting something you actually don’t want to redirect.