Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Redirect Anchor Links to Specific Page WordPress

  • SOLVED

I've searched high and low and I'm beginning to think it's impossible.

Here's what I need to do:

1. I had 33 links that looked similar to this (old HTML site):

http://website.com/store/productCategory.html#product1
http://website.com/store/productCategory.html#product2
http://website.com/store/productCategory.html#product3
...etc...

2. Those pages (or rather anchors to specific places in a single page) have been replaced (using WordPress) like so:

http://website.com/store/productCategory/product1/
http://website.com/store/productCategory/product2/
http://website.com/store/productCategory/product3/
...etc...

3. The old product anchor URLs need to point to the correct new product pages.

I've tried every mod_rewrite I can think of and nothing seems to work since (#) anchors are only used in the browser and not the server.

How can I accomplish what I need? Or am I screwed and pretty much need to remove the /store/ page (and subpages) from WordPress and replace them with the old HTML pages?

Or, can someone give me a solid JS solution for the timebeing?

Answers (6)

2011-08-01

Utkarsh Kukreti answers:

You cannot redirect url's with hash tags, as the hash tag is never sent to the server. You can implement the redirect in javascript, but it'll only work for users who have javascript enabled.


Jason Manheim comments:

What would that js solution be?


Utkarsh Kukreti comments:


<script type="text/javascript">
window.location = "http://website.com/store/productCategory/" + window.location.hash.substring(1) + "/";
</script>


Jason Manheim comments:

I apologize but that doesn't work. My fault. The new product pages are not the same name as the anchor tags.

So, http://website.com/store/productCategory.html#product1 is actually something like http://website.com/store/productCategory/this-is-product-1/


Utkarsh Kukreti comments:

Try this. You can add more redirects to the redirects variable manually.


<script type="text/javascript">
var redirects = {
'product1': 'this-is-product-1/',
'product2': 'this-is-product-22/'
};

window.location = "http://website.com/store/productCategory/" + redirects[window.location.hash.substring(1)];
</script>


Jason Manheim comments:

Doesn't work. It add"undefined to the URL.

When I go to http://website.com/store/productCategory.html#product1 I see it redirect to http://website.com/store/productCategory/this-is-product-1/ for a split second and then it ends up at http://website.com/store/productCategory/this-is-product-1/undefined

Also when going to http://website.com/store/productCategory/ is redirects to http://website.com/store/productCategory/undefined


Utkarsh Kukreti comments:

Try this:

<script type="text/javascript">

var redirects = {
'#product1': 'this-is-product-1/',
'#product2': 'this-is-product-22/'
};

if(redirects[window.location.hash]) {
window.location = "http://website.com/store/productCategory/" + redirects[window.location.hash];
}

</script>


Jason Manheim comments:

Perfect. That'll work for now since not many folks have JS disabled. Thanks a bunch.

2011-08-01

Daniele Raimondi answers:

You can try plugin [[LINK href="wordpress.org/extend/plugins/redirection/"]]redirection[[/LINK]]. It uses regular expressions to catch multiple urls for the old website and redirect them to your new page wit only one rule!


Daniele Raimondi comments:

oh sorry, I missed "#" in your urls. Utkarsh Kukreti is right, hashes never arrive to server...

2011-08-01

Svilen Popov answers:

<blockquote>// $url - get current URL
preg_match_all('/productCategory.html#(.*)/', $url, $match);
header('Location: http://website.com/store/productCategory/'. $match[1][0] .'/');</blockquote>

2011-08-01

Clifford P answers:

http://website.com/store/productCategory.html?action=begin#product1
should function the same as
http://website.com/store/productCategory.html#product1

That doesn't answer your question, but I'll keep looking into it.


Clifford P comments:

Could you just setup a category-products.php file or something that lists all products? And then redirect http://website.com/store/productCategory.html to http://website.com/store/productCategory/ ?

Or even an unlisted page like http://website.com/store/conversion/ as your redirect -- and edit that page in the visual or HTML editor to list just these 33 products.

2011-08-01

Elliott Richmond answers:

If these are separate categories then you'll never achieve what you need too as the links generated by WP will be new queries. If you add tags to individual products you could then create new links from the associated tags

$tags = get_tags();
$html = '<div class="post_tags">';
foreach (get_tags() as $tag){
$tag_link = get_tag_link($tag->term_id);

$html .= "<li><a href='#' title='{$tag->name} Tag' class='{$tag->slug}'>";
$html .= "{$tag->name}</a></li>";
}
$html .= '</div>';
echo $html;


Not sure if I've understood this correctly and I'm sorry if my answer is way off the mark, it's difficult to understand what you're wanting to achieve.

2011-08-01

rizaljohn answers:

Jason,

If I understand it correctly, you can do it in a simple way...:)

Use a [[LINK href="http://wordpress.org/extend/plugins/simple-301-redirects/"]]301 redirects[[/LINK]] plugin that let's your old links point to the new one..

Install it, then input all the old urls then point each to their designated new location... everything will be working great...

Hope it helps!