Forum Moderators: phranque

Message Too Old, No Replies

Redirect 301 shows the variables

redrect, htaccess, RewriteRule

         

chamonix

12:25 pm on Nov 26, 2010 (gmt 0)

10+ Year Member



We have change our htaccess from underscores to hyphens and put in some redrects, however the redirects puts the variables infront of the html like this:

/luk_muk-11087.html => /luk-muk-11087.html?id_lang=1&id_property=087

Here's our htaccess:
-----8<--------------
Options +FollowSymLinks
RewriteEngine On
RewriteBase /

redirect 301 /luk_muk-11087.html http://www.angelshark.co.uk/luk-muk-11087.htm

RewriteRule ^([^/]+)-1([0-9]{1})([0-9]{3}).html$ /test.php?id_lang=$2&id_property=$3


-----8<--------------

How can I get the htaccess file to not add the arguments "?id_lang=1&id_property=087"

Thanks in advance!

g1smd

2:15 pm on Nov 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You need to use RewriteRule syntax for the redirect and for the rewrite.

You also need the [L] flag on every rule, and the [R=301,L] flag for any rules that are redirects.

To clear the parameters, append a question mark to the end of the target URL.

chamonix

4:04 pm on Nov 26, 2010 (gmt 0)

10+ Year Member



Thank you for your reply!

I did this:
-----8<-------------- 
Options +FollowSymLinks
RewriteEngine On
RewriteBase /

redirect 301 /luk_muk-11087.html http://www.angelshark.co.uk/luk-muk-11087.html?

RewriteRule ^([^/]+)-1([0-9]{1})([0-9]{3}).html$ /test.php?id_lang=$2&id_property=$3 [L]
-----8<--------------


And the result is this:

/luk_muk-11087.html => /luk-muk-11087.html?

Which is much better than before, however I still have the question mark there.


You suggested to do a RewriteRule syntax for the redirect, the problem is that we have about 300 links that needs hardcoded 301 redirects, therefore I can't really do a rule. I have to do them one by one.

Any suggestions?

g1smd

4:17 pm on Nov 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This is how you use RewriteRule syntax to make a redirect:

RewriteRule ^<pattern>$ http://www.example.com/<path>? [R=301,L]


If you use RewriteRule for any of your rules, do not use Redirect for any of your rules.
Use RewriteRule for all of the rules.

chamonix

4:10 pm on Nov 29, 2010 (gmt 0)

10+ Year Member



Thanks again for your reply, however I also need to get the variables of those kind of url's

/luk-muk-11087.html?id_lang=1&id_property=087 => /luk-muk-11087.html


So I did this:
-----8<-------------- 
Options +FollowSymLinks
RewriteEngine On
RewriteBase /

RewriteRule ^luk-muk-11087\.html\?$ http://www.angelshark.co.uk/luk-muk-11087.html? [R=301,L]

RewriteRule ^([^/]+)-1([0-9]{1})([0-9]{3}).html$ /accommodation/accommoDetails.php?id_lang=$2&id_property=$3 [NC,L]
-----8<--------------


But this pattern match won't work and I don't understand what I'm doing wrong.

g1smd

6:28 pm on Nov 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The pattern in RewriteRule contains only the path part of the URL.

To examine the query string you need a preceding RewriteCond looking at QUERY_STRING.

jdMorgan

3:49 am on Dec 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, if I'm reading this right, then I'd suggest:

# Externally redirect to remove query strings from hyphenated URLs that resulted from the previously-incorrect .htaccess code
RewriteCond %{QUERY_STRING} ^.
RewriteRule ^luk-muk-(1[0-9]{4})\.html$ http://www.angelshark.co.uk/luk-muk-$1.html? [R=301,L]
#
# Externally redirect to change old underscored URLs to new hyphenated URLs
RewriteRule ^luk_muk-(1[0-9]{4})\.html$ http://www.angelshark.co.uk/luk-muk-$1.html [R=301,L]
#
# Internally rewrite new SEO-friendly hyphenated URLs to script filepath with query parameters
RewriteRule ^(.+)-1([0-9])([0-9]{3})\.html$ /accommodation/accommoDetails.php?id_lang=$2&id_property=$3 [L]

Jim