Apache [removed] fails when using GET parameters, such as ?blah=

后端 未结 3 1606
猫巷女王i
猫巷女王i 2020-12-03 07:27

I\'ve built a new PHP site for a customer and want to redirect the top ranking Google results from the old site structure to the new one.

I\'ve put several dozen Red

相关标签:
3条回答
  • 2020-12-03 08:07

    While Gumbo's answer's reasoning was correct, I could not get his RewriteRule to work.

    Adding another RewriteCond did it. The following was tested and works fine.

    RewriteCond %{REQUEST_URI} /nl/index.php$
    RewriteCond %{QUERY_STRING} ^mID=24511&subID=0$
    RewriteRule ^.*$ http://www.example.com/solutions/printsolutions.html [L,R=301]
    
    0 讨论(0)
  • 2020-12-03 08:09

    Redirect does only operate on the URL paths:

    The old URL-path is a case-sensitive (%-decoded) path beginning with a slash. […]

    So the URL query (the part after the first ? up to the first # after) is not checked.

    But you can use mod_rewrite to do that:

    RewriteCond %{QUERY_STRING} ^mID=24511&subID=0$
    RewriteRule ^nl/index\.php$ http://www.example.com/solutions/printsolutions.html [L,R=301]
    RewriteCond %{QUERY_STRING} ^mID=24512&subID=0$
    RewriteRule ^nl/index\.php$ http://www.example.com/support/koppeling-met-omgeving.html [L,R=301]
    
    0 讨论(0)
  • 2020-12-03 08:10

    Agreeing with both Gumbo's and Martijn's answers ... but:

    Typo in Martijn's, there should be be "^" to start the regular expression for the REQUEST_URI condition:

    RewriteCond %{REQUEST_URI} ^/nl/index.php$
    

    I too could only get Martijn's, not Gumbo's, to work where my .htaccess file was.

    Also, if you don't want the parameter string to be passed on with the rewrite, you should add a "?" on the end of the URL:

    RewriteRule ^.*$ http://www.example.com/solutions/printsolutions.html? [L,R=301]
    

    Otherwise, following Martijn's code, it reads "if your URL is /nl/index.php?mID=24511&subID=0 then redirect to http://www.example.com/solutions/printsolutions.html?mID=24511&subID=0 with a 301 Permanent redirect header and don't process more rules on this URL"

    This may or may not be what you want, and to be fair as a general rule if parameters are not understood they will simply be ignored without doing any harm, so it probably won't matter. However if you're wanting to redirect a human to a new page and want "pretty URLs" then stripping off the parameter string is preferable, so stick the "?" on the end of the destination URL.

    0 讨论(0)
提交回复
热议问题