Page 1 of 1

[Solved] Critical .htaccess problem with 301 rewrites

Posted: Fri Jul 15, 2011 7:55 am
by replytomk3
Hello all,

By using the default .htaccess rewrite rules, a huge problem emerges:

I can't really use redirect 301

Original query gets appended to the resulting redirected url.

For example, a rewrite rule

Code: Select all

redirect 301 /smf/index.php http://forum.mkrd.info/
gives me

Code: Select all

http://forum.mkrd.info/?page=smf/index.php
I have been researching this for (too long at night), and came across the need to clear the previous url in the RewriteRule condition.

I believe the statement at fault to be:

Code: Select all

RewriteRule ^(.+)$ index.php?page=$1 [QSA]
As I need to get some sleep now, the following pages describe how it should look like. The question I have is -- How should the RewriteRule changed to include the question mark that the links below describe, for CMSMS use?

http://drupal.org/node/38960
http://www.webmasterworld.com/apache/4224083.htm
http://stackoverflow.com/questions/3389 ... to-resulti
http://www.seomoz.org/blog/url-rewrites ... t-all-work
http://www.webmasterworld.com/google/4296370.htm
http://www.webmasterworld.com/apache/4080597.htm
http://www.webmasterworld.com/forum92/5663.htm
http://www.webmasterworld.com/apache/3991199.htm

Re: Critical .htaccess problem with 301 rewrites

Posted: Sat Jul 23, 2011 4:23 pm
by replytomk3
Can anyone help?

Re: Critical .htaccess problem with 301 rewrites

Posted: Mon Aug 01, 2011 4:21 pm
by manuel
Hi Tom,

I got redirected from your "help wanted (commercial)" post but thought i would post my reply here to keep problem and (possible) solution together :-)

A little while ago, I also found myself having problems with the .htaccess 301 redirect appending the original query.

When redirecting ALL pages from domain1 to domain2 (without appending the original query), this is what worked for me:

RewriteEngine on
RewriteRule (.*) http://www.domain2.com/ [R=301,L]

Greetings,
Manuel

Re: Critical .htaccess problem with 301 rewrites

Posted: Mon Aug 01, 2011 4:47 pm
by replytomk3
Manuel, what I have tried in implementing your solution does not work. I will post all of my files at the commercial section.

Re: Critical .htaccess problem with 301 rewrites

Posted: Sun Sep 25, 2011 6:20 am
by replytomk3
With about half an hour of tweaking, I got it to work.

To summarize:

Old 301 rewrite rule was something like

Code: Select all

redirect 301 /portals/electronics-as-a-hobby/general-and-misc-2.html http://wiki.mkrd.info/Electronics_as_a_Hobby_portal
To convert to a RewriteRule, use a ^ character. I think that this does pattern matching. Do not use opening "/". Add $ at the end of old page URL.

Any one character off will break the rewrite functionality.

Code: Select all

RewriteRule ^portals/electronics-as-a-hobby/general-and-misc-2.html$ http://wiki.mkrd.info/Electronics_as_a_Hobby_portal [R=301,L]
Is the code to redirect a .html page that used to be within subfolders (as far as could be seen from outside CMSMS) (or parent pages in CMSMS) /portals and /electronics-as....

Re: [Solved] Critical .htaccess problem with 301 rewrites

Posted: Mon Sep 26, 2011 12:44 pm
by manuel
Dear Tom,

Thx for updating this thread, it does indeed work for redirecting a single (pretty) URL!
This is extremely handy for SEO when you change page URL's, article titles (url is generated from title) etc... and the pages have already been indexed (or linked to from other websites)

When using "folders" and you want to make sure url's with or without the trailing slash are being redirected, just add (/?) right before the $:

Code: Select all

RewriteRule ^old/pretty/url(/?)$ http://www.yourwebsite.com/new/pretty/url [R=301,L]
Greetings,
Manuel