Forum Moderators: phranque

Message Too Old, No Replies

Redirection with htaccess

         

toplisek

10:42 am on Feb 10, 2011 (gmt 0)

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



I have tested at CP and also at htaccess

why is redirection not working if I use index.php?



Redirect /sales/index.php?id=myid http://www.mydomain.com/sales/sfurl.html



Why is not working redirection to



http://www.mydomain.com/sales/sfurl.html



but when I use pure pagename like index.php it will redirect. Is GET ID not detected in htaccess?

Is there any option?

g1smd

7:13 pm on Feb 10, 2011 (gmt 0)

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



Redirect cannot "see" query string data.

You need a preceding
RewriteCond
looking at
%{QUERY_STRING}
instead.

Use a
RewriteRule
with the
[R=301,L]
flags, instead of
Redirect
here.

jdMorgan

7:34 pm on Feb 17, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




RewriteCond %{QUERY_STRING} ^id=myid$
RewriteRule ^sales/index\.php$ http://www.mydomain.com/sales/sfurl.html? [R=301,L]

Jim

toplisek

8:23 am on Feb 18, 2011 (gmt 0)

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



I have tested and it works rewrite but not showing page content (error message that page is looping)

Issue is the following:
I have used rule that works in the past but seems working also old link which is not valid for search engines:
RewriteRule ^sales/sfurl\.html$ sales/index.php?id=myid [L]

URL with index.php?id=myid is still working beside my rewrite.

If I use your code it will be not shown page.
What is correct way to stop showing pure index.php?id=myid and if this happens it should show SEF: sales/sfurl.html

jdMorgan

5:32 pm on Feb 18, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Those two rules conflict with each other, writing the URL-path back and forth between the two formats. So the result is an infinite loop.

Too bad you didn't mention your existing rule before we started...

Change the new rule like this to avoid the looping:

# Externally redirect direct client requests for dynamic script filepath back to SE-friendly static URL
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /sales/index\.php\?id=myid\ HTTP/
RewriteRule ^sales/index\.php$ http://www.mydomain.com/sales/sfurl.html? [R=301,L]

Jim

jdMorgan

7:27 pm on Feb 18, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Since it's likely you have many values of "id" and will probably want to rewrite many of them, I'll post this example of a method to use one rule to redirect many of the query-based URLs back to 'friendly' URLs.

This code is more complex than usual, and will require a lot of care and attention to detail. See the comments to clarify the RewriteCond formatting and features. I am assuming that your 'id' values are all-numeric here, but the code can easily be changed if not.

# Externally redirect direct client requests for dynamic script filepaths back to SE-friendly static URLs
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /sales/index\.php\?id=([0-9]+)\ HTTP/
# requested id>new URL... id key>pattern to capture new URL
RewriteCond %1>/sfurlone.html ^1>(.+)$ [OR]
RewriteCond %1>/sfurltwo.html ^2>(.+)$ [OR]
# you can redirect to any filetype or URL-path
RewriteCond %1>/specials/sfurlthree.php ^3>(.+)$ [OR]
# redirect id=99 to /sfurlspl.html
RewriteCond %1>/sfurlspl.html ^99>(.+)$ [OR]
# redirect id=100 to home page "/"
RewriteCond %1>/ ^100>(.+) [OR]
# this last rewritecond must not have an [OR] on it!
RewriteCond %1>sfurllast.html ^101>(.+)$
RewriteRule ^sales/index\.php$ http://www.mydomain.com/sales%1? [R=301,L]

Note that the ">" character is used only as a visual delimiter here. It has no other purpose than to make the code easier to read and to check.

Jim