Forum Moderators: phranque

Message Too Old, No Replies

.htaccess Permanent Redirect issue

Two identical code lines; only one works...

         

Webdeseloper

12:32 pm on Jan 1, 2011 (gmt 0)

10+ Year Member



So permanently renaming a directory or file should be an incredibly straightforward matter.

Old emails sent to customers tend to contain a line directing them to our Terms & Conditions. Now I'm setting up a new system (Zen Cart) and the URL for T&Cs will change.

For customer convenience, I would like to redirect them to the new URL (https://www.example.com/index.php?main_page=conditions) automatically if they enter the old ones (which are example.com/index.php?id=terms_and_conditions and example.com/terms_and_conditions.php). This is regardless of whether the old URLs are entered with "http://" and/or "www.".

Into .htaccess I have added the following code but only the second redirect works!

## Permanently renaming a file or directory
Redirect permanent example.com/index.php?id=terms_and_conditions [example.com...]
Redirect permanent example.com/terms_and_conditions.php [example.com...]

I would be most grateful for any help. Thanks either way!

Happy new year to all!

UPDATE - Possible cause of issue is that zen cart runs a template system where URLS take the query string ?main_page=pagename whereas the old system took the query string ?id=pagename so this might be the cause of the conflict. Just a hunch... Thanks again all

g1smd

1:23 pm on Jan 1, 2011 (gmt 0)

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



The redirect pattern matching can only match the path part of the URL. It cannot ever match the domain name or any parameters.

Use a RewriteRule instead, and add a preceding RewriteCond to test the QUERY_STRING part of the requested URL.

jdMorgan

6:37 pm on Jan 5, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To be clear:

RewriteCond %{QUERY_STRING} ^id=terms_and_conditions$
RewriteRule ^index\.php$ https://www.example.com/index.php?main_page=conditions [R=301,L]
#
RewriteRule ^terms_and_conditions\.php$ https://www.example.com/index.php?main_page=conditions [R=301,L]

Do not mix mod_alias Redirect and RedirectMatch directives with mod_rewrite RewriteRule directives, as doing so can cause unexpected and mysterious problems due to unknown module execution order -- and those problems can quickly ruin your search ranking. So, using this new code will require that you convert all existing Redirect and RedirectMatch directives to RewriteRule directives. Note the differences in syntax and the URL-paths.

Place all external redirect rules ahead of all internal rewrite rules, and order the rules in each group from most-specific patterns and conditions to least-specific patterns and conditions to prevent unexpected malfunctions and exposure of internal server filepaths as URLs to clients. See this thread [webmasterworld.com] in our Apache Forum Library for more info.

Jim

Webdeseloper

1:08 am on Jan 14, 2011 (gmt 0)

10+ Year Member



Thank you both so much for your help! Due to surgery I could not reply earlier. Best wishes, G

Webdeseloper

1:17 am on Jan 14, 2011 (gmt 0)

10+ Year Member



Dear Jim,

I am currently struggling with the Apache documentation in this area.

I have an htaccess file with about 70 lines of code (20 lines are remedial) and wondered if I could ask you to review. I could pay you or donate to webmasterworld (as you tell me). I sincerely doubt it would take you more than 20 minutes to deal with. I don't have a lot of money but do let me know and I will hopefully be able to afford it. Thanks!

Excuse me if I reply slow as recovering from surgery... thanks!

Gautam