Trouble redirecting EZ-page to blog page via .htacess
I've added a Wordpress blog to my ZC site, in a /blog folder, which is working fine.
I've moved some of my EZ-pages to the blog, since they don't really belong on the home page.
Some of these pages were getting google traffic, which I'm trying to redirect (301) to the blog page, but I can't get this working. I'm trying to do this in .htaccess as follows:
Quote:
Redirect 301 /index.php?main_page=page&id=25 /blog/table-tennis-rules/
I've tried the above, I've also tried to redirect to the full domain, or without the trailing "/", but it never works properly. Problem is:
- when it does redirect, it does a 302 instead of a 301
- it seems to re-direct multiple (other) EZ-pages also to the same destination
- it adds a trailing "main_page=page&id=25" to the end of the destination URL
I suspect it's getting screwed up with some of the characters in the source domain, or perhaps it does not like being redirected to what looks like a folder in stead of a file. I can't figure it out!
Can someone advise me how to do this please, it would be a great help!
Thanks a lot!
Re: Trouble redirecting EZ-page to blog page via .htacess
First would be not yet to do a 301 but instead a 302 until the solution is satisfactorily found. This prevents a browser from caching the redirect as permanent instead of temporary.
Next is to identify what all is in your htaccess including the current sequence of the commands.
In the interim you may want to be sure that you are clearing your cache (and maybe cookies) on the browser that you're using between attempts at least.
Re: Trouble redirecting EZ-page to blog page via .htacess
Quote:
Originally Posted by
mc12345678
First would be not yet to do a 301 but instead a 302 until the solution is satisfactorily found. This prevents a browser from caching the redirect as permanent instead of temporary.
Next is to identify what all is in your htaccess including the current sequence of the commands.
In the interim you may want to be sure that you are clearing your cache (and maybe cookies) on the browser that you're using between attempts at least.
hey mate,
i'm going to take you up on that beer when down under!
@mc,
#1 - i do not think the doing a 302 here is at all necessary. the op has stated that they are already getting a 302, so not sure what is the point of changing the htaccess rule.
#2 - as mc has stated, i would post the complete contents of you .htaccess file. i am NOT the apache redirect expert, but it looks to be 1 of 2 choices, a.) there is another rule that is redirecting prior to hitting your rule. b.) your redirect rule is not working/getting ignored because it is not properly constructed or the .htaccess is in the wrong place. i'm betting with option b. my reasoning is simple... if one were to go to your site, the redirect is the same for both of these URLs:
/index.php?main_page=page&id=25
/index.php?main_page=page&id=2545665
which indicates to me the redirect is happening with ZC and not with your htaccess rule.
#3 - definitely not a browser cache/cookie problem.
good luck.
Re: Trouble redirecting EZ-page to blog page via .htacess
You could also edit the /includes/modules/pages/page/header_php.php and add some PHP code to do the redirect.
Something akin to this human-language description:
if (the page number is 25) {
... run the PHP statement: header('Location: /blog/whatever-page-name');
}
Re: Trouble redirecting EZ-page to blog page via .htacess
As for the redirect "issue" of the parameter remaining in the new/redirected url, the htaccess command "Redirect" does not strip the parameter from the url. Therefore to remove it via htaccess, would be better to use "RewriteRule" which also can be written to only redirect id=25 and not include any other page that starts with 25.
Code:
RewriteRule ^index\.php?main_page=page&id=25$ /blog/table-tennis-rules? [L, R=301]
As to temporarily using 302 versus 301, imagine this. An attempt to redirect is entered. A customer goes to the original address. The attempt fails because it was wrong. The customer's browser has been told that the address attempted has been permanently moved to some other non-existent location via cache. How do you tell them to clear their cache? Did your heaers include anything that would pull a new reference/location after some set time out? Does their browser support such a request?
All because wanted to test something...
It's akin to testing that the SSL certificate has been properly installed before changing a site to https and then finding out that it wasn't properly installed and shouldn't have been activated (yet). Why do something as a self test that has to be undone by others when it is possible to temporarily test and then when successful, "lock" it in.
Re: Trouble redirecting EZ-page to blog page via .htacess
Thanks for your time guys, really appreciate it!
Quote:
Originally Posted by
mc12345678
Code:
RewriteRule ^index\.php?main_page=page&id=25$ /blog/table-tennis-rules? [L, R=301]
That causes an internal server error.
Here is the rest of my .htaccess
Code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^tibhar\.com\.au$ [OR]
RewriteCond %{HTTP_HOST} ^www\.tibhar\.com\.au$
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^/?$ "http\:\/\/tabletennisshop\.com\.au\/index\.php\?main_page\=page\&id\=44" [R=301,L]
RewriteCond %{HTTP_HOST} ^xiom\.com\.au$ [OR]
RewriteCond %{HTTP_HOST} ^www\.xiom\.com\.au$
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^/?$ "http\:\/\/tabletennisshop\.com\.au\/index\.php\?main_page\=page\&id\=51" [R=301,L]
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
The existing code refers to two domains (xiom and tibhar) being redirected to a specific page on the site (done through cpanel, looks messy I know), and the last bit redirects http to https.
Re: Trouble redirecting EZ-page to blog page via .htacess
So where in that htaccess was the RewriteRule inserted/tested?
Would advise that since it appears (haven't fully inspected the provided htaccess) desirable to have all domains use https that the RewriteRules direct to https: instead of http: so that there are fewer redirects when accessing that/those uris.
Re: Trouble redirecting EZ-page to blog page via .htacess
I've tried inserting it at the top, and at the bottom, results are the same.
Yes you're right about the other re-directions, they should go right to https. I'm a bit nervous to touch them as they're currently working, and I know very little about this stuff.
Re: Trouble redirecting EZ-page to blog page via .htacess
@ttfan,
i am NOT good at apache redirects. but i am willing to try.
that said, i do not agree with some of the logic going on in this thread. while this could be accomplished from within the ZC core, i am not a fan of doing it that way as it creates more work at upgrade time.
with regards to your htaccess, i would remove the 2nd instance of:
there is no need for it.
in addition, there are 2 problems (that i can see) with regards to mc's rewrite rule. when i tested it, i was also able to reproduce the internal server error. assuming our servers are similarly configured, i think the space in-between the L and the R=301 is what is causing that error. if you remove that space (similar to your other htaccess directives that are working), then the internal server error disappears.
please test that for yourself.
the second problem with that rewrite rule is it does not work. there is something going on with the parameters section of the requesting url that prevents it from matching the rule. i'm still trying to figure it out for myself.
this is a good tool here:
http://htaccess.mwl.be/
i'll keep you posted if i figure anything else out.
best.
Re: Trouble redirecting EZ-page to blog page via .htacess
try this after rewrite engine on:
Code:
RewriteCond %{QUERY_STRING} main_page=page&id=25
RewriteRule ^index\.php$ /blog/table-tennis-rules? [L,R=301]
best.
Re: Trouble redirecting EZ-page to blog page via .htacess
Seems to work well, thank you again!
The only minor issue was that pages of ID 25, or 255 or anything else starting with 25 re-directs to the same page. This was fixed with a $ after the 25.
I've also removed the extra "rewrite engine on", and have my domains now going straight to htpps.
Thanks a lot guys!!! The beers are waiting.
Re: Trouble redirecting EZ-page to blog page via .htacess
One problem with adding the $ at the end of that one parameter is that if there is a link "saved" or accessed that perhaps contains a zenId or has some other parameter on the uri, then a redirect will not occur (strengthening DrByte's recommendation to internally perform the redirect instead of trying to review/validate any possible combination of URIs that might be seen); however, in attempt to account for such on the URI and using an htaccess, might I suggest the following modification/use:
Code:
RewriteCond %{QUERY_STRING} ^(main_page=page&(.*)$|(.*)&main_page=page&(.*)$|(.*)&main_page=page$) [NC]
RewriteCond %{QUERY_STRING} ^(id=52&(.*)$|(.*)&id=52&(.*)$|(.*)&id=52$) [NC]
RewriteRule ^index\.php(.*) /blog/fun-run? [L,R=301]
What this does is support the main_page parameter being at the front, middle or end of the QUERY_STRING *AND* the id being at the front, middle or end (obviously both can not be at the front nor both at the end) of the string. Problem with this also is if say the id parameter is on the uri more than once:
Code:
index.php?main_page=page&id=2&id=52
Then it will still redirect (id=52 is present) even though if using the ZC "redirect", ZC would evaluate the uri as being applicable to id=2 and would not redirect...
To provide a solution that does not require modification of the existing code and therefore a need to be modified in a rebuilt store (but would require copying over to a new installation) the following could be used:
a new file to be loaded into or created in
includes/classes/observers
with the filename possibly:
auto.redirect_specific_ez_pages.php
that has the following content:
Code:
<?php
/**
* This redirects the designated ez-Pages to the chosen location
* @copyright mc12345678 2018 https://mc12345678.com
* @version applicable to ZC 1.5.3 and later. Earlier requires an includes/auto_loaders config.xxx file to invoke this auto.redirect_specific_ez_pages.php file as an observer class
**/
class zcObserverRedirectSpecificEzPages extends base {
function __construct() {
$attachThis = array();
$attachThis[] = 'NOTIFY_HEADER_START_EZPAGE';
$this->attach($this, $attachThis);
}
/**
* Camelized Notifier that is available in ZC 1.5.3 and above and is called when notifier NOTIFY_HEADER_START_EZPAGE is encountered.
* @parameter &$callingClass this modifiable variable provides access to the class that initiated the request.
* @parameter $notifier this contains the string of the name of the notifier that led to executing this function.
**/
function updateNotifyHeaderStartEzPage(&$callingClass, $notifier) {
if (isset($_GET['page']) && (int)$_GET['page'] == 25) {
// Redirects to the identified location, replacing previous headers that may have been sent, using a 301 redirect code
header('Location: /blog/table-tennis-rules/', true, 301);
}
}
/**
* This is the generic observer function that is called if a camelized version of the attached notifier is not found. In versions of ZC pre-1.5.3, this is the
* function that is called for all observers. It is possible in version before ZC 1.5.3 to also receive a non-editable "array" of data. Non-editable meaning
* that changes made here are not carried back to where the notifier is present. The notifier identified here does not pass any additional data and also
* by ZC design is in the global scope of the code, therefore to modify anything presented in the header file, that variable could be made global here
* and its results would be passed back to the line "after" the invoking notifier.
* @parameter &$callingClass this modifiable variable provides access to the class that initiated the request.
* @parameter $notifier this contains the string of the name of the notifier that led to executing this function.
**/
function update(&$callingClass, $notifier) {
if ($notifier === 'NOTIFY_HEADER_START_EZPAGE') {
// Calls the internal class function that handles the request. This way the code is written in one place and changes can be made that are applicable
// to all versions of ZC in which this is ultimately executed.
$this->updateNotifyHeaderStartEzPage($callingClass, $notifier);
}
}
}
Re: Trouble redirecting EZ-page to blog page via .htacess