Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

301 Redirect Rule gives different results on different hosting environment

I have been doing some URL rewriting for a customer in a test environment that work fine in my environment but when I move to theirs I get different results and the rule fails.

I am 301 redirecting legacy dynamic URLs in the following format:

http://www.companyname.com/page.php?pagename=AboutCompany&lang=EN
to
http://www.companyname.com/EN/about-company/

Using the ruleset below this works fine on my 'Heart Internet' shared hosting but fails on their 'Go Daddy' shared hosting (configs below). On the Go Daddy environment the legacy URL gets redirected to:

http://www.companyname.com/GET/about-company/
instead of
http://www.companyname.com/EN/about-company/

where does the 'GET' come? This is a barebones page with no other code influencing these rules.

RewriteCond %{QUERY_STRING} pagename=AboutCompany
RewriteCond %{QUERY_STRING} lang=([^&]+)
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /page.php
RewriteRule ^page.php$ /%1/about-company/? [R=301,L]

RewriteRule ^([A-Za-z]+)/about-company$ page.php?pagename=AboutCompany&lang=$1 [NC,L]
RewriteRule ^([A-Za-z]+)/about-company/$ page.php?pagename=AboutCompany&lang=$1 [NC,L]

The 'lang' querystring differs depending on the user language preference (EN or FR or DE etc...)

Test Enviroment:
Windows
IIS7.5
PHP 5.3.6

Customer Enviroment:
Linux
Apache
PHP 5.2

Thanks, M.

like image 293
MonkeeX Avatar asked Nov 25 '25 02:11

MonkeeX


1 Answers

where does the 'GET' come? This is a barebones page with no other code influencing these rules.

In this snippet of your rules, you have a backreference %1 in your RewriteRule. This references the last () match in the previous RewriteCond, which happens to be (GET|HEAD):

RewriteCond %{QUERY_STRING} lang=([^&]+)
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /page.php
RewriteRule ^page.php$ /%1/about-company/? [R=301,L]

You want to switch those 2 conditions around:

RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /page.php
RewriteCond %{QUERY_STRING} lang=([^&]+)
RewriteRule ^page.php$ /%1/about-company/? [R=301,L]

So that the %1 references ([^&]+)

like image 82
Jon Lin Avatar answered Nov 27 '25 00:11

Jon Lin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!